<?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[const reference error]]></title><description><![CDATA[<p>IIngredientFactory.hxx</p>
<pre><code class="language-cpp">#if !defined (_IINGREDIENTFACTORY_HXX_)
#define _IINGREDIENTFACTORY_HXX_

class CDough;
class CSauce;
class CCheese;
class CPepperoni;

class IIngredientFactory
{
public:
   virtual   CDough*     CreateDough() = 0;
   virtual   CSauce*     CreateCSauce() = 0;
   virtual   CCheese*    CreateCCheese() = 0;
   virtual   CPepperoni* CreatePepperoni() = 0;
};

#endif
</code></pre>
<p>CNYIngredientFactory.hxx</p>
<pre><code class="language-cpp">#if !defined (_CNYINGREDIENTFACTORY_HXX_)
#define _CNYINGREDIENTFACTORY_HXX_
#include &lt;iostream&gt;
#include &quot;IIngredientFactory.hxx&quot;
#include &quot;CPizzaIngredient.hxx&quot;
using namespace std;

class CNYIngredientFactory : public IIngredientFactory
{
public :
   CDough*     CreateDough()     {cout&lt;&lt;&quot;CreateDough NY&quot;&lt;&lt;endl;return new CDough;}
   CSauce*     CreateCSauce()    {cout&lt;&lt;&quot;CreateCSauce NY&quot;&lt;&lt;endl;return new CSauce;}
   CCheese*    CreateCCheese()   {cout&lt;&lt;&quot;CreateCCheese NY&quot;&lt;&lt;endl;return new CCheese;}
   CPepperoni* CreatePepperoni() {cout&lt;&lt;&quot;CreatePepperoni NY&quot;&lt;&lt;endl;return new CPepperoni;}
};

#endif
</code></pre>
<p>CChicagoIngredientFactory.hxx</p>
<pre><code class="language-cpp">#if !defined (_CCHICAGOINGREDIENTFACTORY_HXX_)
#define _CCHICAGOINGREDIENTFACTORY_HXX_
#include &lt;iostream&gt;
#include &quot;IIngredientFactory.hxx&quot;
#include &quot;CPizzaIngredient.hxx&quot;
using namespace std;

class CChicagoIngredientFactory : public IIngredientFactory
{
public :
   CDough*     CreateDough()     {cout&lt;&lt;&quot;CreateDough Chicago&quot;&lt;&lt;endl;return new CDough;}
   CSauce*     CreateCSauce()    {cout&lt;&lt;&quot;CreateCSauce Chicago&quot;&lt;&lt;endl;return new CSauce;}
   CCheese*    CreateCCheese()   {cout&lt;&lt;&quot;CreateCCheese Chicago&quot;&lt;&lt;endl;return new CCheese;}
   CPepperoni* CreatePepperoni() {cout&lt;&lt;&quot;CreatePepperoni Chicago&quot;&lt;&lt;endl;return new CPepperoni;}
};

#endif
</code></pre>
<p>CPizzaIngredient.hxx</p>
<pre><code class="language-cpp">#if !defined (_CPIZZAINGREDIENT_HXX_)
#define _CPIZZAINGREDIENT_HXX_

class CDough
{

};

class CSauce
{

};

class CCheese
{

};

class CPepperoni
{

};
#endif
</code></pre>
<p>CPizzaFactory.hxx</p>
<pre><code class="language-cpp">#if!defined (_CPIZZAFACTORY_HXX_)
#define _CPIZZAFACTORY_HXX_

#include &quot;CNYIngredientFactory.hxx&quot;
#include &quot;CChicagoIngredientFactory.hxx&quot;

#include &lt;string&gt;
using namespace std;

class IIngredientFactory;

class CPizzaFactory
{
public:
   const CPizzaFactory( IIngredientFactory&amp; aFactory)
      :iIngredientFactoryInt(aFactory)
   {

   }
   void CreatePizza()
   {
      iIngredientFactoryInt.CreateDough();//error
      iIngredientFactoryInt.CreateCSauce();//error
      iIngredientFactoryInt.CreateCCheese();//error
      iIngredientFactoryInt.CreatePepperoni();//error

   }
private:
   const IIngredientFactory&amp; iIngredientFactoryInt;//const
};
#endif
</code></pre>
<p>main.cpp</p>
<pre><code class="language-cpp">#include &quot;CPizzaFactory.hxx&quot;
#include &quot;CNYIngredientFactory.hxx&quot;
#include &quot;CChicagoIngredientFactory.hxx&quot;
int main()
{
   const IIngredientFactory* ingredientFactory = new CNYIngredientFactory();
   CPizzaFactory *pizzafactory = new CPizzaFactory(*ingredientFactory);
   pizzafactory-&gt;CreatePizza();

   ingredientFactory = new CChicagoIngredientFactory();
   pizzafactory = new CPizzaFactory(*ingredientFactory);
   pizzafactory-&gt;CreatePizza();
   delete pizzafactory;   
   return 0;
}
</code></pre>
<p>compiler Error 4 mal:</p>
<blockquote>
<p>error C2662: 'IIngredientFactory::CreateDough' : cannot convert 'this' pointer from 'const IIngredientFactory' to 'IIngredientFactory &amp;'</p>
</blockquote>
<p>wieso, ich habe doch iIngredientFactoryInt als const definiert</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/184639/const-reference-error</link><generator>RSS for Node</generator><lastBuildDate>Wed, 23 Sep 2026 20:05:45 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/184639.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 17 Jun 2007 16:18:57 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to const reference error on Sun, 17 Jun 2007 16:18:57 GMT]]></title><description><![CDATA[<p>IIngredientFactory.hxx</p>
<pre><code class="language-cpp">#if !defined (_IINGREDIENTFACTORY_HXX_)
#define _IINGREDIENTFACTORY_HXX_

class CDough;
class CSauce;
class CCheese;
class CPepperoni;

class IIngredientFactory
{
public:
   virtual   CDough*     CreateDough() = 0;
   virtual   CSauce*     CreateCSauce() = 0;
   virtual   CCheese*    CreateCCheese() = 0;
   virtual   CPepperoni* CreatePepperoni() = 0;
};

#endif
</code></pre>
<p>CNYIngredientFactory.hxx</p>
<pre><code class="language-cpp">#if !defined (_CNYINGREDIENTFACTORY_HXX_)
#define _CNYINGREDIENTFACTORY_HXX_
#include &lt;iostream&gt;
#include &quot;IIngredientFactory.hxx&quot;
#include &quot;CPizzaIngredient.hxx&quot;
using namespace std;

class CNYIngredientFactory : public IIngredientFactory
{
public :
   CDough*     CreateDough()     {cout&lt;&lt;&quot;CreateDough NY&quot;&lt;&lt;endl;return new CDough;}
   CSauce*     CreateCSauce()    {cout&lt;&lt;&quot;CreateCSauce NY&quot;&lt;&lt;endl;return new CSauce;}
   CCheese*    CreateCCheese()   {cout&lt;&lt;&quot;CreateCCheese NY&quot;&lt;&lt;endl;return new CCheese;}
   CPepperoni* CreatePepperoni() {cout&lt;&lt;&quot;CreatePepperoni NY&quot;&lt;&lt;endl;return new CPepperoni;}
};

#endif
</code></pre>
<p>CChicagoIngredientFactory.hxx</p>
<pre><code class="language-cpp">#if !defined (_CCHICAGOINGREDIENTFACTORY_HXX_)
#define _CCHICAGOINGREDIENTFACTORY_HXX_
#include &lt;iostream&gt;
#include &quot;IIngredientFactory.hxx&quot;
#include &quot;CPizzaIngredient.hxx&quot;
using namespace std;

class CChicagoIngredientFactory : public IIngredientFactory
{
public :
   CDough*     CreateDough()     {cout&lt;&lt;&quot;CreateDough Chicago&quot;&lt;&lt;endl;return new CDough;}
   CSauce*     CreateCSauce()    {cout&lt;&lt;&quot;CreateCSauce Chicago&quot;&lt;&lt;endl;return new CSauce;}
   CCheese*    CreateCCheese()   {cout&lt;&lt;&quot;CreateCCheese Chicago&quot;&lt;&lt;endl;return new CCheese;}
   CPepperoni* CreatePepperoni() {cout&lt;&lt;&quot;CreatePepperoni Chicago&quot;&lt;&lt;endl;return new CPepperoni;}
};

#endif
</code></pre>
<p>CPizzaIngredient.hxx</p>
<pre><code class="language-cpp">#if !defined (_CPIZZAINGREDIENT_HXX_)
#define _CPIZZAINGREDIENT_HXX_

class CDough
{

};

class CSauce
{

};

class CCheese
{

};

class CPepperoni
{

};
#endif
</code></pre>
<p>CPizzaFactory.hxx</p>
<pre><code class="language-cpp">#if!defined (_CPIZZAFACTORY_HXX_)
#define _CPIZZAFACTORY_HXX_

#include &quot;CNYIngredientFactory.hxx&quot;
#include &quot;CChicagoIngredientFactory.hxx&quot;

#include &lt;string&gt;
using namespace std;

class IIngredientFactory;

class CPizzaFactory
{
public:
   const CPizzaFactory( IIngredientFactory&amp; aFactory)
      :iIngredientFactoryInt(aFactory)
   {

   }
   void CreatePizza()
   {
      iIngredientFactoryInt.CreateDough();//error
      iIngredientFactoryInt.CreateCSauce();//error
      iIngredientFactoryInt.CreateCCheese();//error
      iIngredientFactoryInt.CreatePepperoni();//error

   }
private:
   const IIngredientFactory&amp; iIngredientFactoryInt;//const
};
#endif
</code></pre>
<p>main.cpp</p>
<pre><code class="language-cpp">#include &quot;CPizzaFactory.hxx&quot;
#include &quot;CNYIngredientFactory.hxx&quot;
#include &quot;CChicagoIngredientFactory.hxx&quot;
int main()
{
   const IIngredientFactory* ingredientFactory = new CNYIngredientFactory();
   CPizzaFactory *pizzafactory = new CPizzaFactory(*ingredientFactory);
   pizzafactory-&gt;CreatePizza();

   ingredientFactory = new CChicagoIngredientFactory();
   pizzafactory = new CPizzaFactory(*ingredientFactory);
   pizzafactory-&gt;CreatePizza();
   delete pizzafactory;   
   return 0;
}
</code></pre>
<p>compiler Error 4 mal:</p>
<blockquote>
<p>error C2662: 'IIngredientFactory::CreateDough' : cannot convert 'this' pointer from 'const IIngredientFactory' to 'IIngredientFactory &amp;'</p>
</blockquote>
<p>wieso, ich habe doch iIngredientFactoryInt als const definiert</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1307836</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1307836</guid><dc:creator><![CDATA[netrobot]]></dc:creator><pubDate>Sun, 17 Jun 2007 16:18:57 GMT</pubDate></item><item><title><![CDATA[Reply to const reference error on Sun, 17 Jun 2007 16:26:32 GMT]]></title><description><![CDATA[<p>netrobot schrieb:</p>
<blockquote>
<p>wieso, ich habe doch iIngredientFactoryInt als const definiert</p>
</blockquote>
<p>Genau deswegen kannst du auch keine non-const Member aufrufen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1307843</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1307843</guid><dc:creator><![CDATA[finix]]></dc:creator><pubDate>Sun, 17 Jun 2007 16:26:32 GMT</pubDate></item><item><title><![CDATA[Reply to const reference error on Sun, 17 Jun 2007 16:29:36 GMT]]></title><description><![CDATA[<p>netrobot schrieb:</p>
<blockquote>
<p>[...]<br />
compiler Error 4 mal:</p>
<blockquote>
<p>error C2662: 'IIngredientFactory::CreateDough' : cannot convert 'this' pointer from 'const IIngredientFactory' to 'IIngredientFactory &amp;'</p>
</blockquote>
<p>wieso, ich habe doch iIngredientFactoryInt als const definiert</p>
</blockquote>
<p>Eben. Die Funktionen CreateDough() und Co sind aber non-const und können deshalb natürlich nicht auf einem const-Objekt (oder eine Referenz auf ein solches) aufgerufen werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1307844</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1307844</guid><dc:creator><![CDATA[HumeSikkins]]></dc:creator><pubDate>Sun, 17 Jun 2007 16:29:36 GMT</pubDate></item><item><title><![CDATA[Reply to const reference error on Sun, 17 Jun 2007 16:51:07 GMT]]></title><description><![CDATA[<p>danke, ich dachte immer, const object heisst ich darf interne Daten nicht ändern, aber die Methoden müssen nicht const sein</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1307862</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1307862</guid><dc:creator><![CDATA[netrobot]]></dc:creator><pubDate>Sun, 17 Jun 2007 16:51:07 GMT</pubDate></item><item><title><![CDATA[Reply to const reference error on Mon, 18 Jun 2007 00:03:33 GMT]]></title><description><![CDATA[<p>netrobot schrieb:</p>
<blockquote>
<p>danke, ich dachte immer, const object heisst ich darf interne Daten nicht ändern, aber die Methoden müssen nicht const sein</p>
</blockquote>
<p>Hi,</p>
<p>ein Objekt darf natürlich auch non-const-Funktionen besitzen ... aber Du darfst für const&amp;-Objekte eben nur solche aufrufen, die Du explizit const gemacht hast.<br />
Wie soll der Compiler sonst feststellen, ob es sich um eine Funktion handelt, die den inneren Zustand des Objekts ändert, oder nicht ? Denk dran: Nicht immer hat der COmpiler die Implementierung der Klassenfunktionen zur Hand (z.B. man wenn diese aus einer Lib lädt).</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1308099</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1308099</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Mon, 18 Jun 2007 00:03:33 GMT</pubDate></item></channel></rss>