const reference error
-
IIngredientFactory.hxx
#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; }; #endifCNYIngredientFactory.hxx
#if !defined (_CNYINGREDIENTFACTORY_HXX_) #define _CNYINGREDIENTFACTORY_HXX_ #include <iostream> #include "IIngredientFactory.hxx" #include "CPizzaIngredient.hxx" using namespace std; class CNYIngredientFactory : public IIngredientFactory { public : CDough* CreateDough() {cout<<"CreateDough NY"<<endl;return new CDough;} CSauce* CreateCSauce() {cout<<"CreateCSauce NY"<<endl;return new CSauce;} CCheese* CreateCCheese() {cout<<"CreateCCheese NY"<<endl;return new CCheese;} CPepperoni* CreatePepperoni() {cout<<"CreatePepperoni NY"<<endl;return new CPepperoni;} }; #endifCChicagoIngredientFactory.hxx
#if !defined (_CCHICAGOINGREDIENTFACTORY_HXX_) #define _CCHICAGOINGREDIENTFACTORY_HXX_ #include <iostream> #include "IIngredientFactory.hxx" #include "CPizzaIngredient.hxx" using namespace std; class CChicagoIngredientFactory : public IIngredientFactory { public : CDough* CreateDough() {cout<<"CreateDough Chicago"<<endl;return new CDough;} CSauce* CreateCSauce() {cout<<"CreateCSauce Chicago"<<endl;return new CSauce;} CCheese* CreateCCheese() {cout<<"CreateCCheese Chicago"<<endl;return new CCheese;} CPepperoni* CreatePepperoni() {cout<<"CreatePepperoni Chicago"<<endl;return new CPepperoni;} }; #endifCPizzaIngredient.hxx
#if !defined (_CPIZZAINGREDIENT_HXX_) #define _CPIZZAINGREDIENT_HXX_ class CDough { }; class CSauce { }; class CCheese { }; class CPepperoni { }; #endifCPizzaFactory.hxx
#if!defined (_CPIZZAFACTORY_HXX_) #define _CPIZZAFACTORY_HXX_ #include "CNYIngredientFactory.hxx" #include "CChicagoIngredientFactory.hxx" #include <string> using namespace std; class IIngredientFactory; class CPizzaFactory { public: const CPizzaFactory( IIngredientFactory& aFactory) :iIngredientFactoryInt(aFactory) { } void CreatePizza() { iIngredientFactoryInt.CreateDough();//error iIngredientFactoryInt.CreateCSauce();//error iIngredientFactoryInt.CreateCCheese();//error iIngredientFactoryInt.CreatePepperoni();//error } private: const IIngredientFactory& iIngredientFactoryInt;//const }; #endifmain.cpp
#include "CPizzaFactory.hxx" #include "CNYIngredientFactory.hxx" #include "CChicagoIngredientFactory.hxx" int main() { const IIngredientFactory* ingredientFactory = new CNYIngredientFactory(); CPizzaFactory *pizzafactory = new CPizzaFactory(*ingredientFactory); pizzafactory->CreatePizza(); ingredientFactory = new CChicagoIngredientFactory(); pizzafactory = new CPizzaFactory(*ingredientFactory); pizzafactory->CreatePizza(); delete pizzafactory; return 0; }compiler Error 4 mal:
error C2662: 'IIngredientFactory::CreateDough' : cannot convert 'this' pointer from 'const IIngredientFactory' to 'IIngredientFactory &'
wieso, ich habe doch iIngredientFactoryInt als const definiert
-
netrobot schrieb:
wieso, ich habe doch iIngredientFactoryInt als const definiert
Genau deswegen kannst du auch keine non-const Member aufrufen.
-
netrobot schrieb:
[...]
compiler Error 4 mal:error C2662: 'IIngredientFactory::CreateDough' : cannot convert 'this' pointer from 'const IIngredientFactory' to 'IIngredientFactory &'
wieso, ich habe doch iIngredientFactoryInt als const definiert
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.
-
danke, ich dachte immer, const object heisst ich darf interne Daten nicht ändern, aber die Methoden müssen nicht const sein
-
netrobot schrieb:
danke, ich dachte immer, const object heisst ich darf interne Daten nicht ändern, aber die Methoden müssen nicht const sein
Hi,
ein Objekt darf natürlich auch non-const-Funktionen besitzen ... aber Du darfst für const&-Objekte eben nur solche aufrufen, die Du explizit const gemacht hast.
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).Gruß,
Simon2.