I'm trying to add a Player to a factory in this example. I'm fairly new to pointers and don't get what I'm doing wrong.
in my factory class I have:
void Factory::addPlayer(const Player& player)
{
m_player.push_back(player);
}
and in the program I'm trying to add it as such:
Factory* fact = new Factory();
Player* c = new Player(1, 2, 100, "Name");
fact->addPlayer(*c);
However, when I debug, the 'c' instance is unchanged.
Have I not referenced properly? If anyone could help or point in the right direction I would appreciate any help.
What happens is that an std::vector holds values, not references or pointers. So when you do this:
m_player.push_back(player);
the vector stores its own copy of the Player passed to it. Once you have done that, no action taken on the elements of the vector should affect whatever c points to.
Related
The goal is that every time I create an object of my Car class, a pointer of this object is then created and placed in the "car registry".
This is what I have:
class Car {
private:
string color = "blah blah";
string make = "blah blah";
string model = "blah blah";
public:
Car(CarRegisterManager* carRegisterManager) {
// This is where I want to "register" any object of this class with a pointer
// to reference of this created object inserted into the CarRegisterManager's vector or
// registered cars.
carRegisterManager->registerCar( /*pointer goes here*/ );
}
}
class CarRegisterManager {
private:
std::vector<Car> registeredCars_;
public:
void registerCar(Car* car) {
registeredCars_.push_back(car);
}
}
int main() {
CarRegisterManager carRegisterManager;
CarRegisterManager* p_carRegisterManager = &carRegisterManager;
Car hondaCivic1(p_carRegisterManager); // When this is created, I want a pointer to it registered.
}
As you can see, part of my solution was to upon creation of the CarRegisterManager to create a pointer to the manager object to include as a parameter in the constructor of any Car object, then having the constructor do something with that. I know I need to use a "this" or something to indicate that this created Car object needs a pointer created for it, and then this pointer is put into the CarRegisterManager's registerCar function.
I'm still struggling with the concept of using keywords new and this, if that is indeed what I would need to use here. I apologize if this is answered somewhere else. I legitimately searched for a while before posting this. I may not be using the correct key terms to set these things up.
Thanks.
You already know the answer, because you state it in your question:
I know I need to use a "this" or something to indicate that this created Car object needs a pointer created for it, and then this pointer is put into the CarRegisterManager's registerCar function.
You can use the literal this pointer, eg:
Car(CarRegisterManager* carRegisterManager) {
carRegisterManager->registerCar(this);
}
Code Summary:
Need to pass object of Camera From TestCamera.cpp to Test1.cpp.
I created a object of class into TestCamera.cpp
Now, I want to pass this to Test1, so I passed this to another object.
Anyhow I am getting error of "Camera*" into Test1.cpp .
I am new to CPP. Is it perfect way to pass another class & print it's pointer value?
TestCamera.cpp
Camera * cameraClient=static_cast<Camera *>();
(Test1 *)->captureImage(*cameraClient*);
Test1.cpp
int Test1::captureImage(const Camera* Cam) {
jint lResult;
jlong ad = (jlong)&Cam;
LOGI("Test1: pointer : APP: %ld",ad);
i am not getting your whole code, but just giving you hint that you are passing it wrong way..
you need to pass like this:
(Test1 *)->captureImage(cameraClient);
but, i doubt because you are not allocating memory to cameraClient.
below is extra advise, which is out of your question:
in C++, when you want to create an object of a class dynamically, then you need to allocate memory using new like below:
Camera * cameraClient= new Camera;
which instantiate class of Camera & then pass it to CaptureImage if Test1 class already instantiated..
The code snippet in TestCamera.cpp is not right, when you say that you are creating an object then you need to either use new operator or define an object as below
Camera * cameraClientPtr=new Camera; // For default constructor
Camera cameraClientObj;
The way you invoke method captureImage also not right. If you have Test1 pointer created using any of the aforementioned way then you invoke the method as below,
Test1Ptr->captureImage(cameraClientPtr);
Test1Obj.captureImage(&cameraClientObj);
I have 3 classes. DrawGameComp' and 'GameComp' where 'GameComp' is the base class of 'DrawGameComp'. I have an array of pointers in Game class which is the controlling class. '
GameComp * components[]; From the main I have to create a dynamic instance of Game and store add new objects of GameComp and DrawGameComp to the array of pointers of type GameComp.
Game Game1(2);
Game1.Add(new GameComponent);
Game1.Add(new DrawableGameComponent);
I'v done this part in the main. Because from the main I have to invoke Add passing object as the parameter. When i store these objects I also want assign an id of 1 to the first object and an id of 2 to the second object. How can i include that too.
The Add() function of my Game class is as follows
void Game::Add(GameComponent*)
{
components[0]=GameComp;
componentCount++;
}
but it give me error. I have tried so hard. But I couldn't. Also how do I invoke the Display() member function of these objects in the Array? is it this way?
components[0]->Display();
The Add method should look like:
void Game::Add(GameComponent* comp)
{
components[componentCount++] = comp;
}
Make sure you zero out componentCount in the constructor.
Using the array:
components[i]->DoSomething();
1) You probably meant to write the following:
void Game::Add(GameComponent* comp)
{
components[componentCount++] = comp;
}
2) components[0]->Display() will work, if display is a member function of GameComponent class.
first of all, excuse my poor english.
Well, i'm tryng to make a simple game in C++ with Allegro's library, but i don't know how to create an object array wich can be accesed from all clases.
To be more exactly, i have this code in the main() function of the principal class:
CBody **objs = new CBody*[n];
objs[0] = new CBall(320,240);
objs[1] = new CRing(500,120);
Then i need to make a function in CBall's class to check collision with CRing instance. Something like:
bool CBall::CheckRingCol(){
return (colCircle(myX,myY,myRadious,objs[1]->myX,objs[1]->myY,objs[1]->myRadious));
}
Any ideas?
Thanks you, guys!
First, do not use double pointer madness. You don't need it.
You have an array of CBody, which presumably is a Base class (helps to post more code)
#include <memory>
typedef shared_ptr<CBody> spBody;
vector<spBody> bodies;
bodies.emplace_back( spBody(new CBall(320, 140)) ); // don't need to bother about delete when you use a shared_ptr
bodies.emplace_back( spBody(new CRing(500, 120)) );
Who calls the ball-ring collision?
You could pass the ring as an argument to bool CBall::CheckRingCol(const CRing*) const.
When compiling my C++ program I receive no errors, however within unordered_map the hash function fails, attempting to mod by 0. (Line 345 of hashtable_policy.h of stl)
I've found a fix, but don't know why I'm having the problem to begin with.
My struct looks like this, (Sorry for the specific code.)
struct Player {
private:
Entity& entity = entityManager->create();
public:
Player() {
entity.addComponent(new PositionComponent(0, 0)); // Add component uses the unordered map.
}
};
Player playerOne; // Error perpetuates through constructor.
However, if I declare playerOne as a pointer, like so:
Player* playerOne;
and then call:
playerOne = new Player();
I do not have any issues.
I've been searching - with no success. What could I be doing wrong?
When you use a Player as a global, you've no idea if the entityManager (presumably another global) has been initialised yet - the order of initialisation of globals isn't defined.
When you use the pointer and initialise it with new (in main(), I presume), all the globals have been created by then, so the code works.
This highlights one of the reasons why global variables are a bad idea.