C++: virtual function calling parent [duplicate] - c++

This question already has answers here:
What is object slicing?
(18 answers)
Closed 6 years ago.
#include <iostream>
using namespace std;
struct A
{
virtual int func(void) { return 0; }
};
struct B : A
{
int func(void) { return 1; }
};
int main()
{
A b = B();
cout << b.func() << endl;
}
I was expecting the output to be 1 but as most of you will know its 0.
what i want to achieve in my actual code is something along these lines.
struct A
{
virtual int operator() (int i);
};
struct B : A
{
int operator() (int i) { return i*2; }
};
struct C : A
{
int operator() (int i) { return i*3; }
};
struct x
{
A test;
};
So my container won't be able to tell before hand if it will hold a A, B or C, but should behave differently still.
Is there a way to achieve the functionality as I anticipated it work??

A b = B();: you're constructing an object of type A, using the assignment operator/copy constructor (through copy elision) from a B object, but seen as a reference to A (which explains why it compiles without errors)
This isn't polymorphism. b is still of type A. The link to B has been lost.
That would do it:
A *b = new B();
now
cout << b->func() << endl;
triggers polymorphism/virtual function and yields 1 as expected.

C++ virtual functions only works for references/pointers, which means indirections.
A b = B();
This creates an object of type A, not of type B. No indirection, therefor only the function in A is called.

Related

C++ - calling a member function of another class using a pointer

I'm testing, trying to call a member function being passed as a parameter,
the member function has to be one of another class.
this is an example, which gives an error:
"pointer-to-member selection class types are incompatible ("B" and
"A")"
This is the code, what am I doing wrong?
#include <iostream>
using namespace std;
class A {
private:
public:
void fA(int x) {
cout << "hello" << endl;
}
void fB(int x) {
cout << "good bye" << endl;
}
A() {
}
};
class B {
private:
void (A:: * f)(int) = NULL;
public:
B(void (A:: * f)(int)) {
this->f = f;
}
void call() {
(this->*f)(10); //What's wrong here?
}
};
A a = A();
B b = B(&(a.fA));
B b2 = B(&(a.fB));
int main(void) {
b.call();
b2.call();
}
&(a.fA) is not legal C++ syntax. &A::fA is. As you can see, there is no object of type A anywhere of this syntax. &A::fA is just a pointer to a member function, not a pointer-to-member-together-with-an-object combo.
Now in order to call that pointer-to-member, you need an object of class A. In class B, you don't have any. You need to get one in there somehow, and call the function this way:
(a->*f)(10);
where a is a pointer to that object of class A.

Why does std::bind prevent late binding when using pass-by-reference? [duplicate]

This question already has an answer here:
Does std::bind discard type information of parameters in C++11?
(1 answer)
Closed 5 years ago.
I have a base class, a derived class, and a virtual member function. I also have a function which takes a base class reference and makes a polymorphic call to the member function:
#include <iostream>
#include <functional>
class Base
{
public:
Base() {}
virtual int getnum() { return 1; }
};
class Derived : public Base
{
public:
Derived() {}
virtual int getnum() { return 2; }
};
int getnumref(Base& b) { return b.getnum(); }
int main()
{
Derived d;
Base& bref = d;
std::cout << getnumref(bref) << std::endl;
}
Here, late binding occurs, and the output is 2.
But if I now add the following lines to the main() function in order to pre-define the argument to the function, and then call it:
std::function<int()> boundgetnumref = std::bind(getnumref, bref);
std::cout << boundgetnumref() << std::endl;
then the output of the last line is 1, i.e. here, early binding occurs, and the member function of the base class is called.
If I use pointers, i.e.
//...
int getnumptr(Base* b) { return b->getnum(); }
//...
int main()
{
Derived d;
Base* bptr = &d;
std::cout << getnumptr(bptr) << std::endl;
std::function<int()> boundgetnumptr = std::bind(getnumptr, bptr);
std::cout << boundgetnumptr() << std::endl;
}
then the output of both cout calls is 2.
Why does early binding take place when I use pass-by-reference together with std::bind, and not otherwise?
std::bind stores captured arguments by value causing a slicing copy of Derived to Base.
If you just pass std::reference_wrapper (a pointer) that would copy the pointer, so that slicing copy does not happen:
std::function<int()> boundgetnumref = std::bind(getnumref, std::ref(bref));
Prefer lambdas though, they are the best practice: easier to write, read and more efficient:
auto boundgetnumref = [&bref]() { return getnumref(breg); }

Access members of a class within a different class in c++

I am trying to understand object oriented programming using c++. The following is a minimal example for which the result is not what I naively expect:
#include <iostream>
class B {
public:
B (int val) : val(val) {;}
int get_val() { return val; }
int set_val(int a) { val = a; }
private:
int val;
};
class A {
public:
A (B b) : b(b) {;}
B get_b() { return b; }
private:
B b;
};
int main(){
B b_main(5);
std::cout << b_main.get_val() << std::endl; // Prints 5, which makes sense
A a_main(b_main);
std::cout << a_main.get_b().get_val() << std::endl; // Prints 5, which makes sense
a_main.get_b().set_val(2);
std::cout << a_main.get_b().get_val() << std::endl; // Why does this not print 2?
return 0;
}
The last cout statement does not make sense to me. In the second to last line, I set the value of the object to be 2, so why does this not print 2? Looking at some similar questions on Stack Exchange, I found some suggestions to make A and B be friends of each other. I tried adding friend class A in class B and friend class B in class A, but this did not work. In my understanding, adding the friend statements should be unnecessary since I have the get_b() method in class A. I found some suggestions to try passing the object of type B in by reference to the constructor of A: A (B& b) : b(b) {;} but this did not work either.
Can anyone explain to me why the program is not producing the intended result and also how to obtain the desired result (that is, the last cout statement prints 2)?
Note: I also experimented with the following. I made the private variable b of class A be public:
#include <iostream>
class B {
public:
B (int val) : val(val) {;}
int get_val() { return val; }
int set_val(int a) { val = a; }
private:
int val;
};
class A {
public:
A (B b) : b(b) {;}
B b; // This is now public
//B get_b() { return b; } // This is no longer needed
private:
};
int main(){
B bmain(5);
std::cout << bmain.get_val() << std::endl;
A amain(bmain);
std::cout << amain.b.get_val() << std::endl;
amain.b.set_val(2);
std::cout << amain.b.get_val() << std::endl; // Works!
return 0;
}
And now I obtain the desired result. Is this how the code should be implemented as opposed to the first code snippet? I would like to have a get_b() method as in the first code snippet, but if this is not the correct way of going about this, please let me know.
In the second to last line, I set the value of the object to be 2, so why does this not print 2?
Because you return a copy of the B object in a_main with the get_b() method. What happens is that the b variable in a_main is copied, i.e. another object of class B, identical to the b member, is created, and returned to the caller. Then, that new B object is modified. But it has no connection to the original b in a_main. This has little to do with visibility and member access.
However, in the second example, you expose the b member in a_main and directly operate on that object without making a copy of it, thus the successful result. What the public modifier changes is that it allows you to access the b object directly, hence the effect.
I found some suggestions to try passing the object of type B in by reference to the constructor of A: A (B& b) : b(b) {;} but this did not work either.
That isn't going to work. What happens when you do so, is that the A::b is initialized using the value that is passed by reference, true. But the reference only leads to no additional copy of b passed to the constructor being made. This reference does not create a link between the b passed to the constructor and A::b. It's on the other end, so to say.
By the way, A (B& b) : b(b) {;} that the c'tor parameter name is identical to the member name is a bad practice. It's a good idea to have them named similarly, but still, add e.g. an underscore: A (B& _b) : b(_b) {;}
If you want to achieve the same result in the first snippet, return a reference to b like so:
B& get_b() { return b; }
Still, this is undesirable, because you expose a private member of class A just to allow clients of A to modify a certain property of that member. Better provide a method in A to set the val property of A::b without giving full access to A::b.
Definitely see this: What's the difference between passing by reference vs. passing by value?
and maybe this: Java and C++ pass by value and pass by reference
because I have a feel that you're coming from Java and expect pass-by-reference in C++ by default.
get_b returns a copy of your private variable b, not the actual variable. If you want to be able to access it, you need to return a reference to b so that the returned value can be manipulated. Your get_b definition should look like this:
B& get_b() { return b; }
if that is what you expect to do. However, this is not usually a desirable solution. If you are going to be actively changing the value of b you should write a set_b function to manipulate the variable. And if you are really working with the variable a lot, reading and writing values to it, you should keep it public for fast access.
Just for the sake of completeness, you can solve this problem as a C programing problem rather than using all the fancy references in C++ programing. When you get b_main from a_main, the returned object does not occupy the same memory address.
#include <iostream>
class B {
public:
B (int val) : val(val) {;}
int get_val() { return val; }
int set_val(int a) { val = a; }
private:
int val;
};
class A {
public:
A (B b) : b(b) {;}
B get_b() { return b; }
private:
B b;
};
int main(){
B b_main(5);
B* addrb = &b_main;
std::cout << b_main.get_val() << std::endl; // Prints 5, which makes sense
std::cout<<"Address of b_main: "<<addrb<<std::endl;
A a_main(b_main);
B bt = a_main.get_b();
addrb = &(bt);
std::cout << a_main.get_b().get_val() << std::endl; // Prints 5, which makes sense
std::cout<<"Address of a_main.get_b(): "<<addrb<<std::endl;
a_main.get_b().set_val(2);
std::cout << a_main.get_b().get_val() << std::endl; // Why does this not print 2?
return 0;
}
Notice the difference in address of the new cout statements. One way to fix this is to return pointers rather than b itself. i.e.
#include <iostream>
class B {
public:
B (int val) : val(val) {;}
int get_val() { return val; }
int set_val(int a) { val = a; }
private:
int val;
};
class A {
public:
A (B b) : b(b) {;}
B* get_b() { return &b; }
private:
B b;
};
int main(){
B b_main(5);
//B* addrb = &b_main;
std::cout << b_main.get_val() << std::endl; // Prints 5, which makes sense
//std::cout<<"Address of b_main: "<<addrb<<std::endl;
A a_main(b_main);
//B bt = a_main.get_b();
//addrb = &(bt);
std::cout << a_main.get_b()->get_val() << std::endl; // Prints 5, which makes sense
//std::cout<<"Address of a_main.get_b(): "<<addrb<<std::endl;
a_main.get_b()->set_val(2);
std::cout << a_main.get_b()->get_val() << std::endl; // Why does this not print 2?
return 0;
}

C++ inheritance, calling the given classes function instead of its parent?

Really bad title, couldn't think of how to word it, sorry.
So say I had the following code:
class A {
virtual int getSize() {
return 0;
}
}
class B : public A {
int getSize() {
return 32;
}
}
void doStuff(A a) {
std::cout << a.getSize() << std::endl;
}
int main() {
B b;
doStuff(b);
}
It would print out 0, however I want it to print out 32. In other words, I want to pass it the class and it prints out that classes function, so I could create a class C, where the size is 64, and if I pass that C instance to the doStuff function, I want it to print 64.
Is there any way I can do this in C++, would I have to use templates or some fancy C++ feature I don't know about?
A one-byte patch:
void doStuff(A &a) {
std::cout << a.getSize() << std::endl;
}
Your version takes the argument by value, which means that the function makes a copy of b (a copy which is an A) and then calls the copy's getSize(). In this version, the function takes the argument by reference, and calls b's own getSize(), which is B::getSize().
You should use pointers, or even better: smart pointers! That way, the function of the runtime type gets called. It's a basic example of polymorhpism. If you want to avoid pointers, Beta's slicing approach is equally valid.
#include <iostream>
#include <memory>
class A {
virtual int getSize() {
return 0;
}
}
class B : public A {
virtual int getSize() {
return 32;
}
}
void doStuff(std::shared_ptr<A> a) {
std::cout << a->getSize() << std::endl;
}
int main() {
std::shared_ptr<A> b(new B());
doStuff(b); // Will output '32'.
}
This should correctly call the function as implemented by B.
Slicing the object is one approach, and in addition I think you're asking for, I think, a pretty straightforward use of polymorphism in C++. http://www.cplusplus.com/doc/tutorial/polymorphism/
That's almost immediately applicable, just call your class A Shape, and B and C could be Square and Triangle. Your DoStuff function could take a pointer to a Shape, then you can pass it a triangle or a square, and when you deference the Shape in the function, it will call the correct function.
So you'd have (also you need to make the members public, I think):
class A {
public:
virtual int getSize() {
return 0;
}
};
class B : public A {
public:
int getSize() {
return 32;
}
};
void doStuff(A* a) {
std::cout << a->getSize() << std::endl;
}
int main() {
B b;
doStuff(&b);
}

How to make an array of structs? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I would like to know if I can make an array of structs, then use it in a foreach loop.
struct A { int a; };
struct B { int b; };
struct C { float c; };
And then:
int main()
{
A a; B b; C c;
//Here I want to make an array of a, b and c,
//then use it in for loop with the ints and floats.
}`
You can't safely make an array of unrelated types in C++.
If you have the need to put objects together in a container, then they are probably somewhat related : you should think about adding a common base class to A, B and C, and then manipulate an array (or vector...) of (smart) base class pointers.
However, think about the possible alternative of defining a type encapsulating your struct, instead of declaring this "array" :
struct T {
A obj1;
B obj2;
C obj3;
};
Note :
If you really need type erasure, then boost::any or boost::variant are very handy to encapsulate objects or any type.
Of course you can, but you cannot make an array of unrelated types/structs without using external libraries:
#include <iostream>
struct Person
{
std::string name;
int age;
std::string job;
};
int main()
{
Person people[10];
//I am a normal one.
for(int i=0; i<10; ++i)
{
people[i].name = "usar";
people[i].age = i;
people[i].job = "Astronaut";
}
//foreach loop
for(Person p: people)
{
std::cout << "Name: "<< p.name << "\n";
std::cout << "Age: "<< p.age << "\n";
std::cout << "Job: "<< p.job << "\n";
}
return 0;
}
Yes Just create it just as other types. Assume that we have a struct of Car
struct Car
{
int engine;
string brandName;
};
You can create it like
Car list[2]={{3000, "BMW"},{2200, "Mercedes-Benz"}};
or just
Car list[2];
If you want to have "array" of different types, it is technically possible in C++ though not simple. At least not for novice C++ programmers. First of all you would need to use non trivial data type like boost::variant or std::tuple, second - accessing that data will require template metaprogramming, which is also not easy for beginners. But most probably your problem has simpler solution and can be solved more easily, you are just looking into wrong direction.
struct Base
{
virtual ~Base(){}
virtual int GetInt()
{
throw "Not implemented";
return 0;
}
virtual float GetFloat()
{
throw "Not implemented";
return 0.0f;
};
};
struct A : public Base
{
int a;
virtual int GetInt()
{
return a;
}
};
struct B : public Base
{
int b;
virtual int GetInt()
{
return b;
}
};
struct C : public Base
{
float c;
virtual float GetFloat()
{
return c;
}
};
int main()
{
A a; B b; C c;
Base* array[ 3 ] = { &a, &b, &c };
}