Multiple Inheritance?
I was working in my C++ book and learning about multiple inheritance. For one of the exercises, I had to make two classes (CBaseA and CBaseB) and a class that inherits both of them (CMulti). CBaseA and CBaseB each had two functions: CBaseA had fA() and fCommon, and CBaseB had fB() and fCommon. All each function did was print that it had been called.
I wrote up my program, and CMulti inherited both classes as public.
There were 4 errors when I tried to use my CMulti, so I changed my CMulti instance''s declaration from CMulti myC(); to CMulti myC;
The body of the main is:
CMulti myC; // was CMulti myC();
myC.fA();
myC.fB();
myC.CBaseA::fCommon();
But my question is, what difference does it make when I don''t put the empty parenthesis?
With the empty parentheses the compiler thinks it is a forward function declaration of a function that takes no parameters and returns a CMulti object.
This is inside the body of my main()
I''m wondering why there''s a difference when I try to call the constructor like that.
I''m wondering why there''s a difference when I try to call the constructor like that.
Were the errors along the lines of:
undefined constructor / function
or
no constructor defined taking void arguments?
I think the compiler, when presented with "CMulti myC ();" is looking for the constructor "CMulti::CMulti (void)" (and not finding it) whereas "CMulti myC;" the compiler is using the default constructor (and finding it, sort of, as the compiler will generate the default constructor if no constructors are defined).
I think you''d need to verify the above with the ANSI C++ standard, but it''s all I can think of right now.
Skizz
undefined constructor / function
or
no constructor defined taking void arguments?
I think the compiler, when presented with "CMulti myC ();" is looking for the constructor "CMulti::CMulti (void)" (and not finding it) whereas "CMulti myC;" the compiler is using the default constructor (and finding it, sort of, as the compiler will generate the default constructor if no constructors are defined).
I think you''d need to verify the above with the ANSI C++ standard, but it''s all I can think of right now.
Skizz
The errors are:
C:\C++\Chapter 10\Ex4\Ex4.cpp(42) : error C2228: left of ''.fA'' must have class/struct/union type
C:\C++\Chapter 10\Ex4\Ex4.cpp(43) : error C2228: left of ''.fB'' must have class/struct/union type
C:\C++\Chapter 10\Ex4\Ex4.cpp(44) : error C2228: left of ''.CBaseA'' must have class/struct/union type
C:\C++\Chapter 10\Ex4\Ex4.cpp(44) : error C2653: ''CBaseA'' : is not a class or namespace name
I added a CMulti(){} constructor to the CMulti class, and it still doesn''t work when I say CMulti myC();
C:\C++\Chapter 10\Ex4\Ex4.cpp(42) : error C2228: left of ''.fA'' must have class/struct/union type
C:\C++\Chapter 10\Ex4\Ex4.cpp(43) : error C2228: left of ''.fB'' must have class/struct/union type
C:\C++\Chapter 10\Ex4\Ex4.cpp(44) : error C2228: left of ''.CBaseA'' must have class/struct/union type
C:\C++\Chapter 10\Ex4\Ex4.cpp(44) : error C2653: ''CBaseA'' : is not a class or namespace name
I added a CMulti(){} constructor to the CMulti class, and it still doesn''t work when I say CMulti myC();
quote:Original post by SirGorthon
This is inside the body of my main()
...
I added a CMulti(){} constructor to the CMulti class, and it still doesn''t work when I say CMulti myC();
CMulti myC; is a variable definition.
CMulti myC(); is a function declaration.
There is nothing you can do, short of altering the language itself, to change that fact.
“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan
It still thinks it''s a function declaration if it''s in the body of another function?
Please forgive me if I''m slow
Please forgive me if I''m slow
quote:Original post by SirGorthon
It still thinks it's a function declaration if it's in the body of another function?
Please forgive me if I'm slow
Yes, which would return another syntax error.
I don't see why people think their whould be paranthesis. If it was a function call, it would be ClassA::ClassA(), or ClassAObject::ClassA, not ClassA().
[edited by - thedevdan on June 2, 2004 5:39:54 PM]
Perhaps you''re thinking of CMulti *myC = new CMulti();
[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people
[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement