print pointer address of reference class - c++

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

Related

Can I create a pointer to an object in the constructor of said object?

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

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.

Acces to object array from all classes

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.

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.

C++ changing type in constructor?

I have an abstract class Engine3D and it's structures (Vector3D, etc.) in the header files.
Now, I have an implementation for that class, ConcreteEngine3D : Engine3D. Also, I have other classes like ConcreteVector3D : Vector3D that have additional members and methods that will be used inside ConcreteEngine3D (for the sake of this example, let's say it's float length and calculateLength()).
In main.cpp I have the code:
#include "ConcreteEngine3D.h"
Engine3D * engine;
...
int main(){
engine = new ConcreteEngine3D();
Vector3D* vector = new Vector3D(engine, 10, 5, 2);
}
I want the variable vector be of type ConcreteVector3D*.
I will need that type in ConcreteEngine3D, but in main.cpp I shouldn't even know it's that type and don't have to use extended fields like length. Also, I can't use in main.cpp anything specifically from ConcreteEngine3D.h (only Engine3D.h) - it's for the flexibly, changing the implementation must means only changing the inclusion and line with new ConcreteEngine3D().
I do not want to modify that code above or original headers from Engine3D.
In the constructor of Vector3D I always put a pointer to Engine3D object (and I give here ConcreteEngine3D type).
Maybe can I do something in the constructor of Vector3D to change the type of it?
For example call Vector3D* Engine3D::convert(Vector3D v) inside of the constructor which will be inherited from Engine3D in ConcreteEngine3D (which creates a new ConcreteVector3D object with fields from Vector3D and returns it).
Of course that code doesn't work:
Vector3D::Vector3D(Engine3D *engine){
//how to 'return' or 'convert' the type to the one that returns engine->convert(this);
}
So basically, I want to get the effect of code below but without the line vector = engine->convert(vector) or Vector3D* vector2 = new ConcreteVector3D(engine, 10, 5, 2).
#include "ConcreteEngine3D.h"
Engine3D * engine;
...
int main(){
engine = new ConcreteEngine3D();
Vector3D* vector = new Vector3D(engine, 10, 5, 2); //produces Vector3D, not ConcreteVector3D
vector = engine->convert(vector); //cannot be here! but creates the right object
Vector3D* vector2 = new ConcreteVector3D(engine, 10, 5, 2); //also creates rights object, but cannot be here!
}
Also, I don't want to use factory in Engine3D or ConcreteEngine3D. I want to allow the 'user' create Vector3D just the way I have written in the first code.
It sounds like you would want to use abstract factory pattern to instantiate your objects so that you would not have to directly call constructors of the concrete classes. Then, in case you would want to change the concrete types that implement your interfaces, you would only need to either link in a different implementation of your abstracy factory, or select a proper factory implementation at run time.
Edit: Missed the "don't want to use factory"... Anyway you will need some kind of redirection, because constructor will return the type you instantiate. Closest you can get is probably creating a static factory method "create" to class Vector3D that returns a pointer to Vector3D, but internally creates an instance of concrete implementation class. In case you do that, it is good practice to make the constructor of Vector3D private to prevent creating the vector in "wrong way".
Andrew Tomazos, about the not clear writting - Sorry for that :/
I must agree with Captain Giraffe, that I cannot achieve it without any changes in main.cpp. I used the a little modified factory pattern:
I had to made Vector3D::create() static function which will call Engine3D::convert() and returns it's result. So the code will be:
#include "ConcreteEngine3D.h"
Engine3D * engine;
...
int main(){
engine = new ConcreteEngine3D();
Vector3D vector = Vector3D::create(engine, 10, 5, 2);
}
Inside of Vector3D::create:
return engine->convert(new Vector3D(x,y,z));
Which will produce ConcreteVector3D (because engine is of type ConcreteEngine3D).
Minimal changes in main.cpp, acceptable (no ConcreteVector3D mentioned in the main.cpp).
Once more, thanks for help, to all of you! :-)