Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Backwards inheritance?

Author
Message
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 15th Oct 2010 19:25
This might be beyond most of you (it's beyond me so far). If you have two classes:

Class "B" inherits class "A"'s info and can be accessed from within class "B":

But, I can't do this:

Because "x,y" is part of "B" not "A".

So here is my question: Is there a way to get info from "B" to be used within "A"? Remember, if I try this within "A"

It will fail to compile because class B has yet to be declared.

Any thoughts?

The fastest code is the code never written.
Greg_C
14
Years of Service
User Offline
Joined: 22nd May 2010
Location:
Posted: 15th Oct 2010 20:46 Edited at: 15th Oct 2010 20:47
The whole point of classes is inheritance and polymorphism. If you find yourself wanting to use a function that is in class B in class A then your class hierarchy is all screwed up. The point is to be able to change a function in your base class and all of the classes that inherit from it get those changes as well. So if you want to use a function in A that is in B just make the function in A and then you can use it in A and B.
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 15th Oct 2010 20:56
Quote: "It will fail to compile because class B has yet to be declared."

forward declaration would help on this

But as Greg_C said, doing such thing means that the structure is screwed up, you need to rethink it

also, you probably know this, but anyway, inheritance is not done like:


now B doesn't inherit anything, instead it has an instance of an A object, it should be like:



and accessing is not done like:

instead, directly

as if x was defined inside B, not A

[url][/url]
Nothing's impossible, it's just a matter of time...
Mireben
15
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 15th Oct 2010 21:26
The simplest solution is to parameterize your ShowStupid function and call it with the coordinates:



But if the classes are so interrelated that A needs data from B and B needs data from A, why not combine them into the same class?

Your design is not inheritance as others already pointed out, it is containment: B contains A (also a valid relationship). However it doesn't matter which relationship it is, a contained class will not access the container's data and a base class will not access data of a derived class.
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 16th Oct 2010 17:03
Well, I did a workaround. I put the "showstupid()" in B instead of A.
I was hoping there would be a way to make it happen. I tried to make a class declaration before 'A' the same way you declare functions "void something(void);" prior to making any calls to "something();". It compiled, but failed in execution.

There are C++ keywords like "this" which seems to be kinda redundant because it refers to the class that it's in. I was hoping there was something like "this" for the parent class.

Ah, whishful thinking.

The fastest code is the code never written.
Matty H
15
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 16th Oct 2010 17:25
Quote: "I was hoping there was something like "this" for the parent class."


There is:



Also, you do realise that you are not using inheritance in your examples right? So the above will not work unless you use inheritance:



Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 16th Oct 2010 20:12
@matty halewood
When I get more time, I'll have to study it. There's a lot of things about C++ that I still don't know.

The fastest code is the code never written.
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 18th Oct 2010 18:39
I think I've found something that would do what I was wanting:
http://www.cplusplus.com/doc/tutorial/inheritance/

The keyword is "friend".

The fastest code is the code never written.

Login to post a reply

Server time is: 2024-06-30 11:13:06
Your offset time is: 2024-06-30 11:13:06