Choosing random number for object ID? - c++

I'm implementing a reference counting base class and would like to set uniqe number for each object being created which inherits that interface.
here is a code snippet from that class:
HEADER:
class Object
{
const long long int object_id;
public:
Object();
virtual ~Object();
};
CPP:
Object::Object() : object_id(reinterpret_cast<long long int>(&object_id))
{
}
I'm corious if that's safe approach or not, if not why not?
I didn't use rand and srand function because of 2 reasons:
srand AFAIK is best to use only one time in a project to make random numbers as much as possible random.
this approach is more precise since two objects can not share same memory location.
please advice me.
EDIT:
At which point is member object_id created? inside of constructor or outside (before initializatin list or after) hm hm?
thanks alot!

It's not a safe approach. You haven't considered non-virtual multiple inheritance. It's rare, but legal.
class A : public Object {};
class B : public Object {};
class C : public A, public B {}; // Now C has *two* IDs!

OK this is first time I'm answering to my own question but here is what I did to make unique_id work.
//this is base class which only hold the id and may be inherited only by Object class
struct object_id
{
//friend class Object;
//protected: MAKE IT PUBLIC FOR TESTING PURPOSES! FOR NOW
int id;
object_id() : id(reinterpret_cast<int>(&id)) { } //id will be 100% uniqe
};
//this class inherits virtualy so the id member will be in each derived class only once!
class Object : virtual public object_id //INHERIT PRIVATE LATER, now it's public for testing!
{
public:
Object(){}
virtual ~Object(){}
};
TEST:
//now let's make some diamod inheritance to se if it work:)
class a: public Object{};
class b: public Object{};
class c: public a,b{};
//now let's test it:
int main()
{
c obj;
c ss;
c dd;
cout << endl << obj.id << endl << ss.id << endl << dd.id << endl;
cin.ignore();
return 0;
}
This works just fine and every object now has it's own unique ID!

Related

Acessing subclass variable from base class object

Let's say i'm having the following classes in c++
class Animal{}
class Dog : public Animal {
int barkingVolume;
}
However, i don't have the header file for Dog class. But i have the object of Dog of type Animal at runtime.
The question is how I can access the variable barkingVolume?
Animal animalButDogObject;//someone has set the value at runtime
I need to access barkingVolume from animalButDogObject.
Actual scenario from COM/Directshow: I'm having IBaseFilter object which is of type IVendorFilter(custom filter from 3rd party vendor which extended IBaseFilter). While debugging using Visual studio i can see the type is IVendorFilter and it has variables which i need to change. However i cannot figure out how to do it. I cannot find anything like a reflection/evalutation in CPP
I'd rather comment than post an answer, but can't due to lack of reputation, so here we go.
This is pretty bad, but if you know the exact layout of the class you must access, you could just forward declare the whole thing and reinterpret_cast the object you need.
// FooBar.cpp or something
namespace FooBar
{
class Foo {};
class Bar : public Foo
{
public:
Bar(int ival, float fval) : ival(ival), fval(fval) {}
int ival = 0;
float fval = 0.0f;
};
}
// OtherFile.cpp
class ForwardDeclaredBar
{
public:
int ival;
float fval;
};
#include <iostream>
int main()
{
FooBar::Foo* foo = new FooBar::Bar(3, 2.7f);
auto bar = reinterpret_cast<ForwardDeclaredBar*>(foo);
std::cout << "ival = " << bar->ival << ", fval = " << bar->fval << std::endl; // shows expected values
return 0;
}
Again, this is pretty bad since any changes to the "real" class will mess up your result (reinterpret_cast will just shove whatever data it finds into the format you specified).
There are probably many other reasons which I've no idea about. I'm also unsure how well (if at all) this plays with more complex objects.
declare method on Animal (base) class and overwrite it on Dog (derived) class.
// Base class
class Animal
{
public:
virtual int getBarkingVolume() = 0;
};
// Derived class
class Dog : public Animal
{
private:
int barkingVolume = 8;
public:
int getBarkingVolume()
{
return barkingVolume;
}
};
In main method has Animal (base) type and each derived class that implements the appropriate method (getBarkingVolume) will be compatible with that type.
int main()
{
Animal* animal = new Dog();
std::cout<<"barking: "<< animal->getBarkingVolume();
}

Casting children classes dynamically

I have the following classes:
class Base{
public:
virtual int do_Something();
};
class ChildA : public Base {
public:
int do_Something(); // ChildA does something
};
class ChildB : public Base {
public:
int do_Something(); // ChildB does something
};
In my main.cpp I want to create an object ChildA() or ChildB() based on a specific configuration that the user sets.
My attempt, inside a function main() is:
...
int config = 1; // selected by the user
Base* classe;
std::cout<<typeid(classe).name() << std::endl; // check class name
if (config==0){
classe= new ChildA();
}
else if (config==1){
classe= new ChildB();
std::cout<<typeid(classe).name() << std::endl; // check class name
}
...
Problem
I expect the printed names to be different. Instead, the output is:
P4Base
P4Base
The problem here is classe (Base *) is not polymorphic. It's a simple pointer. You can see it in the output. On Linux with GCC it's: P4Base, pointer to Base
What you want is typeid(*classe).name(). The output is 6ChildB, object of type ChildB.
Polymorphism won't change the type of the pointer (neither static type nor dynamic type) but it changes the dynamic type of the object.
The comment // check class name is wrong. The line
std::cout<<typeid(classe).name() << std::endl;
doesn't check the class but the pointer.

C++ Multiple inheritance problem on accessing second derived class variables

I have a base class A and in this class, there is a vector of the derived class B, and I add class C objects to this list (C is a derived class of B).
But now I am not able to access any variable either from B or C.
My class structure goes like this:
Skill.h
class Skill
{
public:
Skill()
{
}
vector <AttackSkill*> attacks;
vector <UtilitySkill*> utilities;
vector <MoveSkill*> movement;
};
AttackSkill.h
#pragma once
#include "Skill.h"
class AttackSkill :
public Skill
{
public:
AttackSkill()
{
}
string skillName;
int dmgMod;
int baseAcc;
};
One of the skills
#pragma once
#include "AttackSkill.h"
class Axeblade :
public AttackSkill
{
public:
Axeblade()
{
skillName = "Axeblade";
dmgMod = 0;
baseAcc = 72;
}
};
This is how to add new skill
attacks.push_back(new Axeblade);
I just want to be able to access variables.
Example:
"skillPtr" is a pointer to Skill object
for (int i = 0; i < skillPtr->attacks.size(); i++) //No problem here
{
cout << "Skill " << i << ") " << skillPtr->attacks[i]->skillName << endl;
}
Error C2039 'skillName': is not a member of 'Skill'
I am doing some guesswork here since the question is not fully specific but it might help…
Your class Skill contains three vectors (aggregation) and it’s inherited by subclasses defining skill types (inheritance). You should break these two principles apart. There should be one class containing the vectors for a character, let me call it SkillSet. (By the way, you should never issue using namespace in a header file.)
class SkillSet
{
public:
std::vector <AttackSkill *> attacks;
std::vector <UtilitySkill *> utilities;
std::vector <MoveSkill *> movement;
};
Then there will be another base class for all skills which would contain the properties which all skills have:
class Skill
{
public:
std::string skillName;
};
Then you can inherit this new Skill class:
class AttackSkill :
public Skill
{
public:
int dmgMod;
int baseAcc;
};
class Axeblade :
public AttackSkill
{
public:
Axeblade()
{
skillName = "Axeblade";
dmgMod = 0;
baseAcc = 72;
}
};
After creating and filling a SkillSet object:
SkillSet hero0;
hero0.attacks.push_back(new Axeblade);
You can simply access its public members:
std::cout << hero0.attacks[0]->skillName;
See the example of my code.
A few more notes to consider:
You don’t need vectors of pointers, you can put the objects directly to the vector. A vector is not a plain array.
Instead of making the properties public, consider using setters and getters.
Your code contains empty constructors. There is no need to write a constructor that is empty.
On the other hand, if your code contains objects created using new, you should delete them, e.g. in a destructor.

Inheritance initialization in main function in C++ [duplicate]

This question already has answers here:
Polymorphism in C++
(7 answers)
Closed 9 years ago.
From past few weeks I am learning and experimenting inheritance and Polymorphism in C++.
Few syntax always confusing me to understand, mainly object calling from main function.
for eg:
#include <iostream.h>
using namespace std;
class Base
{
public:
Base(){ cout<<"Constructing Base";}
virtual ~Base(){ cout<<"Destroying Base";}
};
class Derive: public Base
{
public:
Derive(){ cout<<"Constructing Derive";}
~Derive(){ cout<<"Destroying Derive";}
};
void main()
{
Base *basePtr = new Derive();
delete basePtr;
}
Here is my question:
What actually happens when Base *basePtr = new Derive(); this syntax is called? and what are the advantages?
As per my knowledge I understood it calls derive class object and stores it in a pointer to base class object. Am I correct? If I am, why are we storing it in base class?
To clear my doubts I went through memory layout of class objects and disassembling, but it confuses me more.
Could anyone tell me how to understand this kind of syntax?
Public inheritance means that every object of the derived class IS at the same time an object of the base class (it provides all the interfaces the base class has). So, when you write:
Base *basePtr = new Derive();
new object of class Derive is created, than the pointer to it is assigned to basePtr and through basePtr you can access all the functionality Base class provides.
And if you then call any of Base class virtual functions like:
basePtr->callSomeVirtualFunction();
the function from the actual object class will be invoked, as it happens with the destructor in the end of your main function.
When you are using pointer to a base class object instead of pointer to a derived one, you are saying that you need only BASIC properties of this derived class.
Hmmm... Pointers are confusing at the beginning.
When you call Base *basePtr = new Derive();, you are creating a Derive object instance and just keeping a "bookmark" of where this object is, but with a Base pointer.
When you do that, the only accessible properties (without a cast) will be from Base class.
Why this is used? To abstract things. Imagine that you are coding something related to mugs, cups, glasses and jugs. Basically all types of those objects are make to store some kind of liquid. So I'll call the base class of LiquidContainer:
class LiquidContainer
{
//...
};
class Mug : public LiquidContainer
{
//...
};
class Glass : public LiquidContainer
{
//...
};
class Cup : public LiquidContainer
{
//...
};
class Jug : public LiquidContainer
{
//...
};
All the others are inherited from LiquidContainer, although the Jug, the Cup and the Mug could be created in a little more sophisticated inheritance tree.
Anyway, the intent of having a base class and using polymorphism is to avoid code replication and to abstract thins, allowing that all the LiquidContainer family be treated almost the same way.
Take by example a more complete class definition.
class LiquidContainer
{
public:
LiquidContainer(unsigned int capacity, unsigned int color) :
mCapacity(capacity),
mColor(color)
{
}
unsigned int getCapacity() { return mCapacity; }
unsigned int getColor() { return mColor; }
virtual char* name() = 0;
protected:
unsigned int mCapacity;
unsigned int mColor;
};
class Mug : public LiquidContainer
{
public:
Mug() :
LiquidContainer( 250, 0xFFFF0000 ) // 250 ml yellow mug!
{
}
virtual char* name() { return "Mug"; }
};
class Glass : public LiquidContainer
{
public:
Glass() :
LiquidContainer( 200, 0x000000FF ) // 200 ml transparent glass!
{
}
virtual char* name() { return "Glass"; }
};
class Cup : public LiquidContainer
{
public:
Cup() :
LiquidContainer( 50, 0xFFFFFF00 ) // 50 ml white cup!
{
}
virtual char* name() { return "Cup"; }
};
class Jug : public LiquidContainer
{
public:
Jug() :
LiquidContainer( 1500, 0x0000FF00 ) // 1.5 l blue Jug!
{
}
virtual char* name() { return "Jug"; }
};
With those class definitions you could do the following test:
#include <iostream>
#include <vector>
int main( int argc, char* argv[] )
{
std::vector< LiquidContainer* > things;
things.push_back( new Mug() );
things.push_back( new Cup() );
things.push_back( new Glass() );
things.push_back( new Jug() );
for ( auto container : things )
{
std::cout << "This is a '" << container->name() << "' with capacity of " << container->getCapacity() << "ml and color " << container->getColor() << std::endl;
}
return 0;
}
This little program outputs
This is a 'Mug' with capacity of 250ml and color 4294901760
This is a 'Cup' with capacity of 50ml and color 4294967040
This is a 'Glass' with capacity of 200ml and color 255
This is a 'Jug' with capacity of 1500ml and color 65280
I hope that this little exercise are enough to show you why the polymorphism is used.
That's called Polymorphism. It means that the object is a Derive as well as Base and can be used as both. For eg. If Dog is the subclass of Animal. Object of dog can be treated as Animal too. All dogs are animal, but not all animals are dog.
So you can call a dog an animal, that's why you can give the address of subclass object(Derive) to superclass pointer(Base). But it'll remain an object of subclass and will function like one. This is just to fool compiler into understanding that it's an object of Base.
Now the benefit is you can have a method which can accept object(or pointer in precise sense) of Base class, but can be passed any of it's subclass. The con here is you can only call methods which are in the base class and may or may not overridden in derive class.

Dependent member initialization, when reorder is not possible

#include <iostream>
class BarParent
{
public:
int x;
virtual void fuz() = 0;
};
class BarChild : public BarParent
{
public:
BarChild(int new_x){x = new_x;}
virtual void fuz(){}
};
class FooParent
{
public:
BarParent* p_barPar;
FooParent (BarChild* new_p_bar)
{
p_barPar = new_p_bar;
std::cout << p_barPar->x << std::endl;
}
};
class FooChild: public FooParent
{
public:
BarChild barChild;
FooChild(int new_x):FooParent(&barChild), barChild(new_x){}
};
int main()
{
FooChild foo(60);
BarChild bar(99);
FooParent fooP(&bar);
}
Output:
-548726160
99
I understand why I am getting this result(undefined behavior), barChild is used before it is initiailized. My question is what is the 'right' to do handle this.
This is a case where design, not code needs to be fixed.
By your own design:
A BarChild must be constructed before FooParent.
A FooParent must be constructed before a FooChild.
A FooChild must be constructed before a BarChild.
When you want both FooParent and FooChild to refer to this same Bar object - as you're attempting in your code - design the parent class to manage it.
One example solution:
FooParent (BarChild* new_p_bar)
{
if ( new_p_bar == NULL )
new_p_bar = new BarChild;
p_barPar = new_p_bar;
std::cout << p_barPar->x << std::endl;
}
Here, FooChild doesn't need its own instance of this object.
Respect the order of initialization.
You can create a function inside BarParent to set the pointer p_barPar. Call the function in the constructor of FooChild.
Is there any reason you can't make barChild (in FooChild) a pointer as well?
I think you will have to find another example: this one is wrong because of the RAIII principle which is not enforced: FooParent holds a pointer on a value which it doesn't control. An interesting example of where this setup would fail is the slicing problem.
quick and dirty solution:
class FooChild: private BarChild, public FooParent
{
public:
FooChild(int new_x): BarChild(new_x), FooParent(this) {}
};