Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

SirGorthon

Multiple Inheritance?

This topic is 5278 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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();

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
It still thinks it''s a function declaration if it''s in the body of another function?

Please forgive me if I''m slow

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!