<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Problem with automatic type conversion]]></title><description><![CDATA[<p>Hello.</p>
<p>Imagine, that I have 2 classes:</p>
<pre><code class="language-cpp">class C
{
private:
  double x;
  [other members]
public:
  inline C(double const &amp;x):x(x) {;} 
};

class PointerToC
{
private:
  C *p;
public:
  inline PointerToC(C &amp;c):p(&amp;c) {;}
}
</code></pre>
<p>Now I want to have automatic type convertion from <code>double</code> to <code>PointerToC</code> :</p>
<pre><code class="language-cpp">double x;
PointerToC pC(x);
</code></pre>
<p>But it does not work. When I change reference type to const:</p>
<pre><code class="language-cpp">class PointerToC
{
private:
  C const *p;
public:
  inline PointerToC(C const &amp;c):p(&amp;c) {;}
}
</code></pre>
<p>the automatic conversion works. Why? What is the difference?</p>
<p>Should I afraid that temporary object <code>C</code> created for such a conversion will be deleted just after the <code>pC</code> construction?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/204890/problem-with-automatic-type-conversion</link><generator>RSS for Node</generator><lastBuildDate>Thu, 08 Oct 2026 18:50:53 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/204890.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 07 Feb 2008 21:33:29 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem with automatic type conversion on Thu, 07 Feb 2008 21:33:29 GMT]]></title><description><![CDATA[<p>Hello.</p>
<p>Imagine, that I have 2 classes:</p>
<pre><code class="language-cpp">class C
{
private:
  double x;
  [other members]
public:
  inline C(double const &amp;x):x(x) {;} 
};

class PointerToC
{
private:
  C *p;
public:
  inline PointerToC(C &amp;c):p(&amp;c) {;}
}
</code></pre>
<p>Now I want to have automatic type convertion from <code>double</code> to <code>PointerToC</code> :</p>
<pre><code class="language-cpp">double x;
PointerToC pC(x);
</code></pre>
<p>But it does not work. When I change reference type to const:</p>
<pre><code class="language-cpp">class PointerToC
{
private:
  C const *p;
public:
  inline PointerToC(C const &amp;c):p(&amp;c) {;}
}
</code></pre>
<p>the automatic conversion works. Why? What is the difference?</p>
<p>Should I afraid that temporary object <code>C</code> created for such a conversion will be deleted just after the <code>pC</code> construction?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1451623</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1451623</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Thu, 07 Feb 2008 21:33:29 GMT</pubDate></item><item><title><![CDATA[Reply to Problem with automatic type conversion on Thu, 07 Feb 2008 22:14:17 GMT]]></title><description><![CDATA[<p>But what will happen if you try to use p later on?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1451642</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1451642</guid><dc:creator><![CDATA[*D*Evil]]></dc:creator><pubDate>Thu, 07 Feb 2008 22:14:17 GMT</pubDate></item><item><title><![CDATA[Reply to Problem with automatic type conversion on Thu, 07 Feb 2008 22:21:27 GMT]]></title><description><![CDATA[<p>(D)Evil schrieb:</p>
<blockquote>
<p>But what will happen if you try to use p later on? ...</p>
</blockquote>
<p>... boom, because your temporary object doesn't exist anymore. Don't rely so much on automatic type conversation. It is better if you have your Code under Control.</p>
<p>And all the inline's before your C'tor's are not necassary.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1451650</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1451650</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Thu, 07 Feb 2008 22:21:27 GMT</pubDate></item><item><title><![CDATA[Reply to Problem with automatic type conversion on Thu, 07 Feb 2008 22:25:34 GMT]]></title><description><![CDATA[<p>POD can simply be copyed ... do not need references ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1451653</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1451653</guid><dc:creator><![CDATA[*D*Evil]]></dc:creator><pubDate>Thu, 07 Feb 2008 22:25:34 GMT</pubDate></item><item><title><![CDATA[Reply to Problem with automatic type conversion on Thu, 07 Feb 2008 22:26:55 GMT]]></title><description><![CDATA[<p>SAn schrieb:</p>
<blockquote>
<pre><code class="language-cpp">class C
{
private:
  double x;
  [other members]
public:
  inline C(double const &amp;x):x(x) {;} 
};

class PointerToC
{
private:
  C *p;
public:
  inline PointerToC(C &amp;c):p(&amp;c) {;}
};

double x;
PointerToC pC(x);
</code></pre>
</blockquote>
<p>Normally this shouldn't work too, because you try to bind a temporary C-Object to an non const-Reference</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1451654</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1451654</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Thu, 07 Feb 2008 22:26:55 GMT</pubDate></item></channel></rss>