Pointer to class???

Pages: 12
No, the problem is main doesn't know what dim is cause it's an attribute of Sina_NS::Sina objects.

PS: The interfaces of your classes collide - both have the public int attributes width and height. Because Sina already has those two, there is no need to declare them again in Dim.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;
namespace Sina_NS {
class Dim;

class Sina {

public:
    int width, height;
	class Dim *dim;
	int area () {return (width * height);}
};
}
using namespace Sina_NS;
class Dim: public Sina{
	
public:
    int width, height;
	int pp () {return (width * height);}
};

int main () {
	dim->width=2;
	return 0;
}


That should do it. It seems that the behavior described by filipe did not hold with your compiler. Maybe it is compiler-specific. Go with this way instead.

But just so you know, if it had worked, the full name of the class would have been Sina_NS::Sina::Dim and not Sina_NS::Dim.

Also note that your declaration of the Dim class is outside the Sina_NS namespace, meaning its full name is just Dim.
Last edited on
1
2
3
4
int main () {
	dim->width=2;
	return 0;
}


That is the problem. dim was never declared.
hanst99 is correct. A forward declaration only tells the compiler about a name. If you need to dereference a pointer (access the object it points to), then you need a full declaration.
So could you give an example that shows the advantage of:
class Foo *ptr;
over
Foo *ptr;
???

Thank you in advance
There is no real advantage, the only reason to ever do that would be a cross reference between classes (Class A has a B object, and Class B has a A object). And even then I'd rather put a class B; forward declaration in front of A instead. But you CAN do it, so if you feel like it, do it. It's not like you'd break anything.
And even then I'd rather put a class B; forward declaration in front of A instead.

Very much agreed.
Thanks guys
Topic archived. No new replies allowed.
Pages: 12