Jump to content
  • Advertisement

Archived

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

Roof Top Pew Wee

Interesting problem with classes and pointers.

This topic is 6231 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''m wondering if this is legal. class engine { int horsePower; }; class car { engine engineInCar; void returnPtrToEngine(engine* ptrToReturnTo) { ptrToReturnTo = &engineInCar; } }; void main() { engine* testEngine; engine testCar; testCar.engineInCar.horsePower = 200; textCar.returnPtrToEngine(testEngine); cout << "This should give me the hp of the test car:" << testEngine->horsePower << endl; } I have tried a program similar to this and it didn''t work. It was rather complex, so I''m not sure where the error was, but I thought to ask if this would work, and if not, why. I am simply tring to get a function that will assign the address of a member to the pointer passed to the function. I could do the assigning outside of the class, but there are going to be some tests that I would like to keep in the class for cleaner code. Thanks in advance for any help. --Vic-- }

Share this post


Link to post
Share on other sites
Advertisement
As you know, changes to a parameter are never reflected on return. The problem there is that you only changed the parameter. You probably want something like this:

void returnPtrToEngine(engine** ptrToReturnTo)
{
*ptrToReturnTo = &engineInCar;
}

testCar.returnPtrToEngine(&testEngine);


or:


engine* returnPtrToEngine()
{
return &engineInCar;
}

testEngine = testCar.returnPtrToEngine();


I normally use the second method, but the first one is more similar to what you already had.

Share this post


Link to post
Share on other sites
1.) The default "visibility" of the components of a class is private - they can not be seen by anything outside the class. If you want classes with default public visibility, create them as structs (they''re the same in C++ other than that):
  
struct engine {
int horsePower; // this is a public variable

};

class car
{
engine engineInCar; // this is a private variable


public:
// this is an _old_ C-style declaration, where you couldn''t return structs

void returnPtrToEngine( engine * ptrToReturnTo );
// this is a C++-style declaration

engine *returnPtrToEngine( void );
};


Your car class doesn''t have a constructor defined, so engineInCar is never set to any useful value. Also, in your main program you declare testCar as an engine. Here''s what you want to do:
  
// struct engine as before


// class car, revised

class car
{
engine engineInCar; // this is a private variable


public:
// this is called a constructor. it creates a new object for you

car( int hp );

// this is an _old_ C-style declaration, where you couldn''t return structs

void returnPtrToEngine( engine * ptrToReturnTo );
// this is a C++-style declaration

engine *returnPtrToEngine( void );
};

// there are two ways to set the horsepower of your car: base

// initializers or the constructor function body

// method 1: base initializers (recommended for simple types)

car::car( int hp ) : engineInCar.horsePower(hp)
{
// constructor body is empty

}

// method 2:

car::car( int hp )
{
engineInCar.horsePower = hp;
}

// other car functions

engine *car::returnPtrToEngine()
{
return engineInCar;
}

#include <iostream>
using std::cout;
int main( int argc, char *argv[] )
{
car testCar(200); // create a car with a 200hp engine

engine *testEngine = testCar.returnPtrToEngine();
cout << "Engine horsepower: " << engine->horsePower << endl;
return 0;
}

Share this post


Link to post
Share on other sites
To BeerHunter

I am looking at the first block of code, and I don''t see much of a difference. It seems to me all you did is pass a pointer of a pointer, rather than just a pointer. Why would this matter? I did the same thing as you, only I put a pointer in the arguments and gave it the address of the engineInCar. I am a litle confused as to why you had to pass a double pointer as the argument.

To Oluseyi:

Hehehe, My mistake on the code. You pointed out three things (No constructor, there are two Engines in main, and that I didn''t declare the members public. The thing is, In my actual code, I did all three of those things correctly. I just threw this code together as an example and I forgot to do those things. What you did is set the pointer to the address of the engine member variable of the car outside of the class. I am trying to find out if it is possible to do inside the class with the returnPtrToEngine(engine* ptrToReturnTo) function.

Share this post


Link to post
Share on other sites
Beerhunter is right, Roof top pew wee (next time choose a shorter pseudo :-]).

It does a LOT of difference to write:

a) void ptrEngine(engine** ptr)
*ptr = &mEngine;

or

b) void ptrEngine(engine* ptr)
ptr = &mEngine;

And the correct version is a). Why? Well, I''ll try to put it short, and if you don''t understand you HAVE to look at it in your C(++) reference manual, ''coz it''s quite important.

b) simply put the value of ptr on the stack. Then your code modify this value (always working on the stack). Result: once you''ve returned from the code, the value of your variable hasn''t change. If you want, you can call it "parameter passed by value".

a) put the address of your pointer on the stack. Then you modifiy the variable pointed by this address, that is: YOUR variable. So when returning you really have changed the value of your engine pointer. Call it "parameter passed by address" if you want.

there''s a third way: references. use this declaration:
void ptrEngine(engine*& ptr)
and ptr can be manipulated like b), but behaves like a) (look at your C++ doc for more details).

I hope I''ve helped.
jods

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!