Jump to content
  • Advertisement
Sign in to follow this  
sushi-one

pointer problem..

This topic is 5414 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

the code below is not working the way I want it to be working. What I want is to be able to get the pointer from class A into class B like this: int* peker2 = peker; now, everything I get is that "peker" is 0xCDCDCDCD, witch is uninitialized..
#include <stdio.h>

class A {
public:
	int* peker;
	void Init();
};

void A::Init() {
	peker = new int[10];

	printf("\nadresse1 = %p\n\n", peker);
}

class B:public A {
public:
	void Init();

	void Open();
};

void B::Open() {
	int* peker2 = peker;

	printf("adresse1 = %p, adresse2 = %p\n", peker, peker2);
}



void main() {
	A* cA;
	B* cB;

	cA = new A;
	cB = new B;

	cA->Init();

	cB->Open();
}

Share this post


Link to post
Share on other sites
Advertisement
You made two objects cA and cB, but called Init only for the cA.. so you should call Init for cB also before you call Open.

You could test it yourself with followin main:



void main() {
A* cA;
B* cB;

cB = new B;
cA = dynamic_cast<A*>(cB);

cA->Init();
cB->Open();
}


Share this post


Link to post
Share on other sites
Looking over the code, your using new but your not using delete! Make sure that once you've finished with the pointers, you delete them.
Now to answer your question. I believe you can use virtual methods here instead of the dynamic cast. Just a sugestion and before you ask, realistically there is no performance hit using virtual methods!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!