| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"stefven blonqhern" |
| Date: |
24 Aug 2007 11:47:03 AM |
| Object: |
two classes #including each other |
hi, i imagine we've all seen this one posted before but i can't get
any solutions to work for me.. for example:
i have two classes MyClass and Shape. MyClass creates Shapes (and
derived classes from Shape) and it also wants to pass a pointer to
itself to a member function of Shape so that it can access MyClass'
members. Pseudo code example:
class MyClass {
void MyFunction{
aShape->DoSomethingToShape(this)
}
private:
Shape* aShape;
}
/////////////
class MyClass; // forward declaration
class Shape { // is a base class
void DoSomethingToShape(MyClass* aClass) {
aClass->some_member; // ** ERROR invalid use of undefined type
'struct MyClass' **
}
I have simplified greatly (possibly too much to make proper sense).
Really i'm trying to avoid passing 10-20 references to MyClass members
because it feels a bit clumsy. I just want to pass a pointer to a
MyClass so that Shape can access what it wants.
Is there a better way?
thanks, stefven.
.
|
|
| User: "Neelesh Bodas" |
|
| Title: Re: two classes #including each other |
24 Aug 2007 12:02:56 PM |
|
|
On Aug 24, 9:47 pm, stefven blonqhern <robo_cree...@yahoo.co.uk>
wrote:
hi, i imagine we've all seen this one posted before but i can't get
any solutions to work for me.. for example:
i have two classes MyClass and Shape. MyClass creates Shapes (and
derived classes from Shape) and it also wants to pass a pointer to
itself to a member function of Shape so that it can access MyClass'
members. Pseudo code example:
class MyClass {
void MyFunction{
aShape->DoSomethingToShape(this)
}
private:
Shape* aShape;
}
/////////////
class MyClass; // forward declaration
class Shape { // is a base class
void DoSomethingToShape(MyClass* aClass) {
aClass->some_member; // ** ERROR invalid use of undefined type
myClass needs to be defined before its members are used.
'struct MyClass' **
}
I have simplified greatly (possibly too much to make proper sense).
Really i'm trying to avoid passing 10-20 references to MyClass members
because it feels a bit clumsy. I just want to pass a pointer to a
MyClass so that Shape can access what it wants.
You can do something like this: (just an example):
//Myclass.h
class Myclass
{
public:
void foo();
};
//Myclass.cpp
#include "Myclass.h"
void Myclass::foo()
{
...
}
//shape.h
class Myclass;
class Shape
{
public:
void DoSomethingToShape(Myclass*);
};
//shape.cpp
#include "shape.h"
#include "myclass.h"
void Shape::DoSomethingToShape(Myclass* aClass)
{
aClass->foo();
}
-N
.
|
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: two classes #including each other |
24 Aug 2007 12:04:33 PM |
|
|
stefven blonqhern wrote:
hi, i imagine we've all seen this one posted before but i can't get
any solutions to work for me.. for example:
i have two classes MyClass and Shape. MyClass creates Shapes (and
derived classes from Shape) and it also wants to pass a pointer to
itself to a member function of Shape so that it can access MyClass'
members. Pseudo code example:
class MyClass {
void MyFunction{
aShape->DoSomethingToShape(this)
}
private:
Shape* aShape;
}
/////////////
class MyClass; // forward declaration
class Shape { // is a base class
void DoSomethingToShape(MyClass* aClass) {
aClass->some_member; // ** ERROR invalid use of undefined type
'struct MyClass' **
}
Pull this function's definition out of the class 'Shape' and
#include <MyClass.h> right in front of it (if you need this to be
in the header). Or just place it in a separate translation unit.
I have simplified greatly (possibly too much to make proper sense).
Really i'm trying to avoid passing 10-20 references to MyClass members
because it feels a bit clumsy. I just want to pass a pointer to a
MyClass so that Shape can access what it wants.
Is there a better way?
Usually, yes. The Java habit of having all functions implemented in
the class definition is not the best to follow when C++ is concerned.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
|
|
|
| User: "stefven blonqhern" |
|
| Title: Re: two classes #including each other |
24 Aug 2007 02:16:55 PM |
|
|
Is there a better way?
Usually, yes. The Java habit of having all functions implemented in
the class definition is not the best to follow when C++ is concerned.
so really i should be separating the interface and the
implementation. Java? never heard of it!
Stefven
.
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: two classes #including each other |
24 Aug 2007 02:27:04 PM |
|
|
stefven blonqhern wrote:
Is there a better way?
Usually, yes. The Java habit of having all functions implemented in
the class definition is not the best to follow when C++ is concerned.
so really i should be separating the interface and the
implementation. Java? never heard of it!
Interface and implemenatation are concepts, they are separate by
nature. You should be separating declarations and definitions.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
|
|
|
| User: "Martijn van Buul" |
|
| Title: Re: two classes #including each other |
25 Aug 2007 03:31:42 AM |
|
|
* Victor Bazarov:
You should be separating declarations and definitions.
Simplifying things again, aren't we? There are various reasons why you
wouldn't want to seperate the definitions, or even *can't* seperate the
definitions. Function inlining comes to mind; in order for this to work
you *NEED* the declaration and the definition to be in the same place.
Templates come to mind, as well.
--
Martijn van Buul -
.
|
|
|
| User: "Pete Becker" |
|
| Title: Re: two classes #including each other |
25 Aug 2007 07:15:18 AM |
|
|
On 2007-08-25 04:31:42 -0400, Martijn van Buul <pino@dohd.org> said:
* Victor Bazarov:
You should be separating declarations and definitions.
Simplifying things again, aren't we? There are various reasons why you
wouldn't want to seperate the definitions, or even *can't* seperate the
definitions. Function inlining comes to mind; in order for this to work
you *NEED* the declaration and the definition to be in the same place.
Templates come to mind, as well.
Well, "in the same place" in the sense that both have to be present if
the function is called. That doesn't mean the function's definition has
to be inside the class definition. It can come later. It also doesn't
mean that the function's definition has to be in the same source file
as the class definition. Some people put them in separate files.
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
.
|
|
|
|
|
|
|
| User: "stefven blonqhern" |
|
| Title: Re: two classes #including each other |
24 Aug 2007 01:13:08 PM |
|
|
okay, thanks for the replies.. naturally i forgot to mention that i'm
using public inheritance. Shape being the base class and all member
functions are virtual.
looks like i have some thinking to do, thanks,
stefven.
.
|
|
|
|
|

|
Related Articles |
|
|