Acessing subclass variable from base class object - c++

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

Related

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.

How come accessing derived class member in base class pointer vector returns an error?

Streamlined Example of the problem:
#include <string>
#include <deque>
#include <iostream>
class Action{
public:
std::string name;
Action(std::string name){
this->name = name;
}
};
class Ability : public Action{
public:
int bar;
Ability(std::string name) : Action(name){}
};
int main(){
std::deque<Action*> foo;
Ability test("asdf");
test.bar = 122;
foo.push_back(&test);
std::cout << foo.at(0)->bar << std::endl;
return 0;
}
This creates an error, that there is no 'bar' member of 'Action'.
I realise that this relates to object slicing and I've attempted to use pointers, which allows the vector to push back the 'Ability' object but I cannot access its 'bar' member.
What am I missing?
First, a word from our sponsor: What is object slicing?
Now that you've read the above link, you can see that no slicing has taken place because the object was not copied into foo, only a pointer to the object was copied. The Ability is still intact, wherever in memory test sits.
But... Foo contains pointers to Action, not Ability. There is no way for a user of Foo to know if any given element of Foo is a reference to an Action, an Ability, or some other subclass of Action that they know absolutely nothing of. Very powerful stuff, the ability to work with something you don't even know exists, but this comes at a price: You have to work with it as something you do know. Users of Foo can only use the interface presented to them, that of Action. There are ways around this, such as dynamic_cast, but in most cases it best to stick with the provided interface and allow an overloaded method or operator to do black magic behind the scenes to do the correct thing for whatever the Action represents. If this means you have to
class Action{
public:
std::string name;
Action(std::string name){
this->name = name;
}
virtual int getbar() = 0; // pure virtual method that all subclasses
// of Action must implement
};
class Ability : public Action{
public:
int bar;
Ability(std::string name) : Action(name){}
int getbar()
{
return bar;
}
};
and later
std::cout << foo.at(0)->getbar() << std::endl;
so be it.

Casting a variable passed by reference in C++

I have some code that passes a variable by reference, but doesn't result in the variable being updated in the calling code as I would expect;
// Interface classes
class Animal{};
class Car{
public:
virtual void testDrive(Animal &animal) = 0;
};
// A specific implementation
class Bear : public Animal{
public:
int testthing = 0;
};
void Ferrari::testDrive(Animal &animal){
Bear b = dynamic_cast<Bear &>(animal);
b.testthing = 1;
}
// Use those classes, doesn't need to know about Bear or Ferrari
int main()
{
// Set up myCar and myAnimal
myCar.testDrive(myAnimal) // ** but myAnimal is unchanged! **
}
I've actually been able to get this to work (myAnimal is updated with testthing = 1) by passing a pointer instead, but I'd be interested to know what's going on here.
As I understand it passing a variable by reference is very closely related to passing a pointer anyway, and "With regard to polymorphism, references work just like pointers"*.
So why does one work and the other not? Is there a simple way to get this to work with a reference?
*Are references and pointers equal with regards to polymorphism?
EDIT: this is just an example to show my meaning, obviously not production code.
Bear b = dynamic_cast<Bear &>(animal); is taking a value copy of the cast value of animal, so modifications on b will not affect the original.
You want Bear& b = dynamic_cast<Bear &>(animal); instead. Then b is itself a reference.
Note that if dynamic_cast fails when taking a reference cast, then std::bad_cast is thrown. You ought to deal with that appropriately.
I'm not 100% sure what the question is. Normal casting works ok:
#include <iostream>
using namespace std;
// Interface classes
class Animal{};
class Car{
public:
virtual void testDrive(Animal &animal) = 0;
};
class Ferrari : public Car {
public:
void testDrive(Animal &animal);
};
// A specific implementation
class Bear : public Animal{
public:
int testthing = 0;
};
void Ferrari::testDrive(Animal &animal){
Bear & b = (Bear &) animal;
b.testthing = 1;
}
// Use those classes, doesn't need to know about Bear or Ferrari
int main()
{
// Set up myCar and myAnimal
Animal myAnimal;
Ferrari myCar ;
myCar.testDrive(myAnimal); // ** but myAnimal is unchanged! **
cout << ((Bear &)myAnimal).testthing ;
}
Prints:
1

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.

Choosing random number for object ID?

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!