Acces to object array from all classes - c++

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.

Related

print pointer address of reference class

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);

Write constructor for a library class that doesn't have one

I am using a struct from a 3rd party library to pass parameters into library functions.
I wish there were a constructor that would allow me to quickly create a struct, but the library doesn't provide one.
Is there some way to define a constructor outside of the library that I can use within my own code? If not, does anybody else see an elegant solution, here?
My motivation is that I would save some speed because I wouldn't have to construct each struct member twice.
Also, my code would be more readable because I could condense struct creation into one line instead of many lines.
I want to go from this:
Point newPoint;
newPoint.x = someXValue;
newPoint.y = someYValue;
To this:
Point newPoint(someXValue, someYValue);
If you're using C++11, you can construct as:
Point newPoint {someXValue, someYValue};
and if you're not, assuming this is a POD structure, you could:
Point newPoint = {someXValue, someYValue};
Use a factory method. But you will get another function.
Point CreatePoint(int x, int y)
{
Point temp;
temp.x=x;temp.y=y;
return temp;
}
This may not be best in performance perspective or readability, but it is the basic, so I just list it here.
class MyPoint : public Point {
public:
MyPoint(int x, int y) { ... }
};
Construct your objects as MyPoint, and you can pass it to the library function directly.

How to store objects of different class in an array of pointers

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.

Boost::python - filling a C++ vector of pointer on object of a class in python

Here is my problem. I am supposed to make a python code able to fill a C++ vector of pointer on object of a class of the c++ code. I am using Boost::Python.
It's look as follow:
C++ code:
class A_Base {}
class A_derived_1 {}
...
class A_derived_N {}
class B {
...
setAderivediList(vector<A *> &_AderivedList){}
}
What I need to do in Python:
B.setAderivediList(_AderivedList)
I cannot modified the method in the C++ code, I can only add other objects in-between #ifdef python_interface in order not to impact other developpers work.
At first, I tried to write a converter from what I found on the Internet, but I failed to make it works for my code. Then I tried to add a _AderivedList in class B and a setter which fill the vector. Though it compile in C++, it dosen't work in Python.
New B class:
class B {
...
setAderivediList(vector<A *> &_AderivedList){}
#ifdef myPython_code
public:
vector<A *> * _AderivedListPy;
setAListPy(A * &my_Aderived){ //Actually, I tried without the reference as well
this->_AderivedListPy->push_back(my_Aderived);
};
#endif
}
In python it becomes:
myB = mydll.B()
Ai = mydll.A_derived_i()
myB.setAListPy(Ai) #stopping here
myB.setAderivediList(myB._AderivedListPy)
If I use the reference, it will throw a "python argument types did not match C++ signature" error message at myB.setAListPy(Ai), without the reference, it will stop at the same line without throwing any error message.
Any clue of how I could improve this or any other way to do it?
Thank you in advance.
Actually, I found a solution to my issue.
By filling the vector (including calling setAderivediList) directly in setAListPy.
I just pass an int my_A_derived to setAListPy (actually, along with other arg required by all A_derived class constructors) and use a if condition on my_A_derived.

Created object isn't being saved. Pass by reference

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.