I have problem with passing array of pointers to a function.
Class CTree01 is child of CDecorationObj.
When I use for loop, it's working fine.
Here's my code:
CTree01 *trees01;
int numTree01 = ...;
trees01 = new CTree01[numTree01];
//main loop
for(int i=0;i<numTree01;i++)
{
{
//Everything is working okay here, renders all trees
glPushMatrix();
trees01[i].DrawOnTerrain(camera);
glPopMatrix();
}
}
but since i replaced for with a function, it's not working anymore:
void DrawDecorationType(CDecorationObj* objs, int number, CCamera *camera)
{
int x,z;
for(int i=0;i<number;i++)
{
{
glPushMatrix();
objs[i].DrawOnTerrain(camera);
glPopMatrix();
}
}
}
//main loop
DrawDecoration(trees01, numTree01, camera);
When I do in that function:
objs[0].DrawOnTerrain(camera);
it works, and it crashes only when I render objects with index > 0 so I think it must be problem with parameter passed to the function.
Let me see if I can explain the problem using couple of simple classes.
Let's say you have:
struct Foo
{
char a;
};
struct Bar : Foo
{
char b;
};
sizeof(Foo) is 1 and sizeof(Bar) is 2.
And you create an array of Bar object using:
Bar* barPtr = new Bar[2];
The layout of memory that barPtr points to looks like:
Bar(0) Bar(1)
| |
v v
+---+---+---+---+
| a | b | a | b |
+---+---+---+---+
If you pass that pointer to a function a Foo*, that function will interpret the memory as (since sizeof(Foo) == 1):
Foo(0)
| Foo(1)
| |
v v
+---+---+
| a | a |
+---+---+
As you can see, Foo(1) is not really a Foo object. It is really the Bar sub-object of Bar(0). This can easily lead to undefined behavior. Depending on the kinds of data a base class and a derived class have, it could easily crash your program.
The key point is that a pointer that points to an array of derived class objects cannot be treated as a pointer that points to an array of base class objects.
Very good explanation from R Sahu. It clearly describes why your code isn't working and deserves to be accepted.
So what can you do instead?
As far as I understand you want to pass all your CTree01 to a function in one call and have that function iterate all your CTree01s.
The only way I know is to make the container (i.e. array, vector) holding the base class pointers instead of CTree01.
Something like this:
class A
{
public:
A() { cout << "A constructor" << endl;}
virtual ~A() { cout << "A destructor" << endl;}
void hello() { cout << "Hello from A" << endl;}
};
class B : public A
{
public:
B() { cout << "B constructor" << endl;}
~B() override { cout << "B destructor" << endl;}
};
void f(array<A*,2>& t)
{
for(auto e : t)
{
e->hello();
}
}
int main()
{
array<A*,2> z; // Base class pointer array
z[0]=new B; // but it can still hold pointers to B
z[1]=new B;
f(z);
delete z[0];
delete z[1];
return 0;
}
or using vector instead of array
class A
{
public:
A() { cout << "A constructor" << endl;}
virtual ~A() { cout << "A destructor" << endl;}
void hello() { cout << "Hello from A" << endl;}
};
class B : public A
{
public:
B() { cout << "B constructor" << endl;}
~B() override { cout << "B destructor" << endl;}
};
void f(vector<A*>& t)
{
for(auto e : t)
{
e->hello();
}
}
int main()
{
vector<A*> z;
z.push_back(new B);
z.push_back(new B);
f(z);
delete z[0];
delete z[1];
return 0;
}
Related
lets say I have a class with 2 constructors like so:
class Foo
{
Foo(int x);
Foo();
...
}
I know that I can call one constructor from another like Foo() : Foo(42) {}, but why shouldn't (or should) I do the following:
Foo() {
Foo(42)
}
What is the difference in these cases?
Some suggest to use an "initializer" method, called from any constructor with their respective arguments, but I am puzzled as to what happens in the case above?
Let's put this example:
#include <iostream>
class B
{
private:
int number;
public:
B()
{
B(1);
}
B(int x) : number(x)
{
std::cout << "Constructor: " << x << std::endl;
}
void print(){
std::cout << "Msg: " << number << std::endl;
}
~B(){std::cout << "Destructor: " << number << std::endl;}
};
int main() {
B b;
b.print();
return 0;
}
Output:
Constructor: 1
Destructor: 1
Msg: 1
Destructor: 1
You are destroying a second object! This is strange, what happens if we use pointers...
#include <iostream>
class B
{
private:
int* arr;
public:
B()
{
B(1);
}
B(int x)
{
std::cout << "Constructor: " << x << std::endl;
arr = new int[x];
}
void print(int n){
std::cout << "Msg: " << arr[n] << std::endl;
}
void set(int n,int val){
arr[n] = val;
}
~B()
{
std::cout << "Destructor: " << arr << std::endl;
delete[] arr;
}
};
int main() {
B b;
b.set(0,14);
b.print(0);
return 0;
}
Constructor: 1
Destructor: 0xc45480
Msg: 14
Destructor: 0xc45480
Look up the pointer addr. They are the same, this means:
We are writing in deleted memory.
We are deleting the same memory twice.
These are two serious problems. You shouldn't do that.
Expression Foo(){Foo(42);} constructs anonymous temporary object that gets destroyed immediately without changing the object being constructed anyhow, while from Foo() : Foo(42){} will initialize the object being constructed.
You should not the following:
Foo() {
Foo(42)
}
When you are in the constructor body the member variables have been just constructed. That's why in C++ initialisation list exists.
The above code is semantically wrong! You are not absolutely using
delegating construction. Instead the statement Foo(42) in the body will just create another object without assigning it to any variable (anonymous variable).
You can imagine something like:
Foo() {
Foo another_obj = Foo(42);
}
In order to use delegating constructor, you must call constructor
in the initialisation list.
Foo() : Foo(42) { }
I am writing code to access private members of a class through another friend class. The below code works
// Example program
#include <iostream>
#include <string>
using namespace std;
class Foo
{
private:
int a;
protected:
public:
friend class Bar;
Foo(int x)
{
a = x ;
}
};
class Bar
{
private:
protected:
public:
int b;
Bar(Foo& f)
{
b = f.a;
cout << "f.a is " << f.a << endl;
}
};
int main()
{
Foo foo(5);
Bar bar(foo);
cout << "Value of variable b is " << bar.b << endl;
}
Above code works fine. However, if I want to access a private variable of Foo through a function in friend class Bar, I am unable to. See code below
#include <iostream>
#include <string>
using namespace std;
class Foo
{
private:
int a;
protected:
public:
friend class Bar;
Foo(int x)
{
a = x ;
}
};
class Bar
{
private:
protected:
public:
int b;
Bar(Foo& f)
{
b = f.a;
}
void printvariable(void)
{
cout << "f.a is " << f.a << endl;
}
};
int main()
{
Foo foo(5);
Bar bar(foo);
cout << "Value of variable b is " << bar.b << endl;
}
I totally understand why execution fails on the
void printvariable(void)
{
cout << "f.a is " << f.a << endl;
}
function since f is not in scope for the function. However, since I am passing Foo f in the constructor for Bar b, I am hoping to write code that will allow me to access members in Foo without passing Foo f to the function printvariable() again.
What is the most efficient way to write this code?
You can keep the reference to f. The code should be:
class Bar
{
private:
protected:
public:
int b;
Foo& f_ref;
Bar(Foo& f)
:f_ref(f)
{
b = f.a;
}
void printvariable(void)
{
cout << "f.a is " << f_ref.a << endl;
}
};
TEST!
You can do it like this, but if I were you I'd write some getters, also – class friendship isn't really recommended.
class Bar {
public:
Foo& ref;
Bar(Foo& f)
: ref { f }
{ }
void printvariable() {
cout << "f.a is " << ref.a << endl;
}
};
Btw there's no reason to add void in brackets in C++, it lost its meaning from C and has no effect by now.
You are wrong in one point. You are indeed passing a reference to f in the ctor, but the constructor and whole class Bar does not remember a whole object of f. In your original code, the constructor only makes the Bar object remember the int a part of the object, so only that little bit is later accessible:
class Foo
{
...
friend class Bar;
...
};
class Bar
{
...
int b;
Bar(Foo& f)
{
b = f.a; // <=--- HERE
}
void printvariable(void)
{
cout << "f.a is " << b << endl; // <-- now it refers B
}
Please note how your ctor of Bar only reads f.a and stores it in b. From now on, the Bar object only remembers b and that's all. You can freely access the b in printvariable. However, it will not be the a-taken-from-f. It will be b, that was set to the same value as f.a during constructor. Since that point of time, b and f.a are totally separate. That's how value copying works.
To make Bar remember whole f, you have to, well, remember whole f:
class Bar
{
...
Foo wholeThing;
Bar(Foo& f)
{
wholeThing = f; // <=--- HERE
}
void printvariable(void)
{
cout << "f.a is " << wholeThing.a << endl;
}
However, again, there's a catch: now since wholeThing is of type Foo, the constructor will actually make a copy of that object during wholeThing=f. Just the same as it was when b=f.a, but now it remembers a copy of whole f.
Of course, it's only matter of type. You can store a reference instead of whole-Foo, but it needs a bit different initialization syntax:
class Bar
{
...
Foo& wholeThing;
Bar(Foo& f) :
wholeThing(f) // <=--- HERE
{
// <=--- empty
}
void printvariable(void)
{
cout << "f.a is " << wholeThing.a << endl;
}
Is there any way in C++ to collect different types of classes and call their methods?
What I want to do is as below,
template<namespace T>
class A
{
A method_A1(T a)
{
...
}
void method_A2(int aa)
{
...
}
...
};
class B
{
...
};
class C
{
...
};
class D
{
...
};
A<B> *b;
A<C> *c;
A<D> *d;
b -> method_A2(3);
c -> method_A2(5);
In this code object b,c,d they are totally different object, right? Not related.
But I want to bind them with a array, so...
z[0] = b;
z[1] = c;
z[2] = d;
like this.
I found some solutions, however the solutions are only for collecting different types. (using void* arrays or vectors for inherited objects) I also wanna access to their methods.
z[0] -> method_A2(3);
z[1] -> method_A3(5);
like this.
In this case how should I do?
Thanks in advance.
typedef boost::variant<A<B>, A<C>, A<D>> AVariant;
std::array<AVariant, 3> z;
z[0] = *b;
z[1] = *c;
z[2] = *d;
Then you can inspect each element's type if needed, or "visit" them using boost::static_visitor as shown here: http://www.boost.org/doc/libs/release/doc/html/variant.html
Why you don't use inheritance and polymorphisms. I have posted an example of what exactly can be a solution of your problem. See the main function:
#include <iostream>
class weapon {
public:
int fireRate;
int bulletDamage;
int range;
int activeBullet;
public:
virtual void fire(void) {std::cout << "machine " << '\n';}
virtual ~weapon() {std::cout << "destructor is virtual" << '\n';}
};
class machineGun: public weapon {
public:
void fire(void) {std::cout << "machine gun firing" << '\n';}
~machineGun(void) { std::cout << "machine gun destroyed" << '\n';}
};
class flamer: public weapon {
public:
void fire(void) {std::cout << "flamer firing" << '\n';}
~flamer(void) {std::cout << "flamer destroyed" << '\n';}
};
int main(void)
{
const int count = 2;
weapon *weapons[count];
machineGun *a = new machineGun();
flamer *b = new flamer();
weapons[0] = a;
weapons[1] = b;
weapons[0]->fire();
weapons[1]->fire();
delete a;
delete b;
}
If you don't want to change your classes' hierarchy, you can try having an array of callable objects. Something like:
#include <iostream>
#include <functional>
#include <array>
class A
{
public:
void Foo(int a)
{
std::cout << "Foo " << a << std::endl;
}
};
class B
{
public:
void Bar(int a)
{
std::cout << "Bar " << a << std::endl;
}
};
int main()
{
using namespace std::placeholders;
A a;
B b;
auto a_func = std::bind(&A::Foo, a, _1);
auto b_func = std::bind(&B::Bar, b, _1);
std::array<std::function<void(int)>, 2> arr = {
std::bind(&A::Foo, a, _1),
std::bind(&B::Bar, b, _1)
};
arr[0](1);
arr[1](2);
return 0;
}
BTW, this will only work if you use compiler with full C++11 support.
Lets say I have implemented the following classes
class A
{
public:
virtual void printA()
{
cout << "Hi from A!" << endl;
}
};
class B : public A
{
public:
virtual void printB()
{
cout << "Hi from B!" << endl;
}
};
class C : public B
{
public:
void printC()
{
cout << "Hi from C!" << endl;
}
};
Lets also say I have created a std::vector<A *> vec that contains random amount of objects instantiated from A, B, and C. Now lets say I am forced to iterate through all the objects in vec but only call objects that have the printC() method (i.e C instances). What is the best way to do this?
int main()
{
std::vector<A *> vec;
....
// insert random objects from both A, B and C into vec
....
for(vector<A *>::iterator x = vec.begin();
x != vec.end();
x++)
{
if(dynamic_cast<C *>(*x) != 0) // 1. is this OK?
(*x)->printC();
else
(*x)->printA(); // 2. is this also OK?
}
}
Is 1 Ok? And if so is this the best practice?
Also will 2 cause problems in the case of C instances?
Maybe these are stupid questions, but Im quite new to C++ and how polymorphism works in C++ is very strange to me. Thanks
I think you mean the following
#include <iostream>
#include <vector>
int main()
{
class A
{
public:
virtual ~A() = default;
virtual void print() const
{
std::cout << "Hi from A!" << std::endl;
}
};
class B : public A
{
public:
void print() const
{
std::cout << "Hi from B!" << std::endl;
}
};
class C : public B
{
public:
void print() const
{
std::cout << "Hi from C!" << std::endl;
}
};
std::vector<A *> v = { new A(), new B(), new C() };
for ( A *p : v ) p->print();
return 0;
}
The output is
Hi from A!
Hi from B!
Hi from C!
1 won't work, since *x has type A*, and A doesn't have a printC member. It should be:
if (C * c = dynamic_cast<C *>(*x)) {
c->printC();
}
2 is fine, but doesn't match your description; you say you want to "only call objects that have the printC() method", while this calls printA() on the other objects.
This does seem like an odd design though; you'd usually define a single virtual function, implemented by each class to do the right thing for that class, then call that unconditionally for everything.
The following piece of code gets compiled under g++ 4.6.3 for Linux
#include <iostream>
class A {
public:
int x;
std::string c;
A(int x,std::string c):x(10),c("Hi"){
}
~A(){
std::cout << "Deleting A()" << std::endl;
}
};
class B : public A {
public:
B():A(20,"Hello"){
}
~B(){
std::cout << "Deleting B()" << std::endl;
}
};
int main(){
B o;
std::cout << o.x << std::endl;
std::cout << o.c << std::endl;
return(0);
}
but it does not do what is supposed to do, the type B is not able to change the values of that 2 variables that it inherit from A.
Any explanations about why this doesn't work properly ?
Your base constructor takes those values...and completely disregards them!
Change this:
A(int x,std::string c):x(10),c("Hi"){}
to this:
A(int x,std::string c):x(x),c(c){}
There seems to be some confusion about what you want and how to achieve this.
If I got you right this is what you want:
class A {
public:
int x;
std::string c;
//default initization of A
A():x(10), c("Hi") {}
//initializing the values of A via parameters
A(int x,std::string c):x(x),c(c){}
~A(){
std::cout << "Deleting A()" << std::endl;
}
};
class B : public A {
public:
B():A(20,"Hello"){
}
~B(){
std::cout << "Deleting B()" << std::endl;
}
};
So in this example:
int main()
{
A a;
A a1(2, "foo");
B b;
return 0;
}
a.x == 10, a.c == "Hi"
a1.x == 2, a1.c == "foo"
b.x == 20, b.c == "Hello"
OK, I don't understand what exactly you want and why, but here's a suggestion, with C++11, you can do the following:
struct Base {
int a;
float b;
};
struct Derived: public Base {
Derived(): Base{1,1.0} {}
};
int main() {
Derived d;
}
as long as the base is a POD type.
I'd still prefer A(int x = 10,std::string c = std::string("Hi")):x(x),c(c){...} though.
IMHO, you need to review if you really need that much control over your base class in the first place. You're not really supposed to micro-manage a class from the outside like that, it's an indication of a flaw in your class hierarchy.