I have the following code:
struct A
{
virtual void foo() {std::cout << "A\n";}
};
struct B : public A
{
virtual void foo() {std::cout << "B\n";}
};
void bar(A * a)
{
a->foo();
}
Without changing this code, is it possible to cast bp pointer to B, so calling bar would print "A"?
int main()
{
B * bp = new B();
bar(/* do somethig*/ bp);
return 0;
}
Tried every cast I remebered:
int main()
{
B * bp = new B();
bar((A*)bp);
bar(static_cast<A*>(bp));
bar(reinterpret_cast<A*>(bp));
bar(dynamic_cast<A*>(bp));
return 0;
}
You could make a shim wrapper around B, and have the shim's virtual function dispatched to BWrap::foo() call directly to A::foo();.
There's not really any point in the example to carrying along the B& member variable reference, but for more interesting examples there may be a use case.
struct BWrap : public A
{
B& b;
BWrap(B& bb) : b{bb} {}
virtual void foo() { b.A::foo(); }
};
int main()
{
B* bp = new B();
BWrap bw{*bp};
bar(&bw);
}
If you insist on the A object being a base class subobject of a B object and on not modifying the first code snippet at all, then the only solution is to add an even more derived class that can override the virtual call as explained in the answer by #Eljay (which I completely forgot to think about when first writing this answer).
Other options are to create a complete A object, not a B object, or to modify bar to do a call without virtual dispatch by using a qualified name:
a->A::foo();
All of the casts you are showing have the same effect as the implicit conversion, except for reinterpret_cast which will cause undefined behavior when used this way.
Related
pass * as a function parameter
#include <iostream>
class A {
public:
A() {}
};
class B : public A{
public:
B() {}
};
void foo(A* a) {
a = new B();
}
void ff() {
A a;
foo(&a);
}
pass ** as a function parameter
#include <iostream>
class A {
public:
A() {}
};
class B : public A{
public:
B() {}
};
void foo(A** a) {
*a = new B();
}
void ff() {
A* a;
foo(&a);
}
When I see leveldb, I am in a puzzle about this.
The specific code is the following.
https://github.com/google/leveldb/blob/master/db/db_impl.cc#L1537
I think it's ok to use FileLock lock
Then pass &lock to LockFile(const std::string& filename, FileLock* lock)
LockFile function is this
Passing double pointer and just a pointer are not the same
void foo(A* a) {
a = new B();
}
Change an input pointer to another value. The result will not reflect in the callee ff().
It's merely change the argument.
void foo(A** a) {
*a = new B();
}
Change a pointer to another pointer and change the value, which is "the another pointer", of this pointer points to . The result will reflect in ff()
According to the ff(), I assume you have a follow up question which need to operate a after foo() is called. So second one might be a proper way.
For FileLock, I believe you're referring to this implementation.
It's using double pointer because it wants to replace the entire content of FileLock.
Live demo
Followup: Why it chose using double pointer instead of pass by reference?
Because reference is not possible to be replaced, my assumption is that it chose it chose to replace the whole content which is simpler than make the implemention complex for re-initialization.
I have the following structure of classes:
struct A {
virtual void function() = 0;
};
struct B : A {
void function() override {
// ...implementation...
}
};
struct C {
virtual A a() = 0;
};
struct D : public C {
A a() {
return B{};
}
}
the main idea is: I do not really care what class will be returned from D::a, the only property I want it to have is a function() member-function. So I denoted the return type as the abstract class A which has this property as virtual, and then try to return B object which is derived from A. But this does not work, giving error: invalid abstract return type. Could you please suggest a way to achieve the desired effect in C++?
In C++, you can't do polymorphism with objects, because objects could have different sizes, and in C++ types have value semantics. You can, however, do polymorphism with references, and pointers. So your function could be:
A* a();
// or
A& a();
This does mean that you may have problems with ownership. You can also return a smart pointer instead:
unique_ptr<A> a();
You can't create objects of abstract types, you can create pointers of them. As Ayxan said, you can use also smart pointers, but I suggest you use shared_ptr because it's more comfortable.
Use dynamic_cast to convert B to A.
You forgot to add ; to the end of the third struct.
Here is the correct code:
#include <iostream>
struct A {
virtual void function() = 0;
};
struct B : A {
void function() override {
std::cout << "EEAAAHHHH\n";
}
};
struct C {
virtual A* a() = 0;
};
struct D : public C {
A* a() {
B *a = new B;
A *b = dynamic_cast<A*>(a);
return b;
}
};
int main()
{
D d;
A* a = d.a();
a->function();
return 0;
}
If you compile and run the code, you can see that the output is "EEAAAHHHH".
I'm happy to help you, and I wish you success.
Look at this excerpt of a program.
I see that cout << obj->foo(); call is not polymorphic. Actually, it is obvious, because it has no virtual specificator.
But I am confused with cout << ((B*)obj)->foo(); Why the program does not use the B's definition of virtual function and will call the third version of foo()?
#include <iostream>
using namespace std;
class A{
public:
int foo(){ return 1; }
};
class B: public A{
public:
virtual int foo(){ return 2; }
};
class C: public B{
public:
int foo(){ return 3; }
};
int main() {
A* obj = new C;
cout << obj->foo();
cout << ((B*)obj)->foo();
cout << ((C*)obj)->foo();
return 0;
}
A::foo() is not virtual. Calling foo() via an A* pointer (or A& reference) will call A::foo() directly without any polymorphic dispatch.
B::foo() is virtual. Calling foo() via a B* pointer (or B& reference) will dispatch the call to the most derived implementation of foo() that exists in the object that the B* (or B&) refers to.
C derives from B, and C::foo() overrides B::foo(), and obj points to a C object, which is why C::foo() gets called by polymorphic dispatch when foo() is called via a B* or C* pointer (or a B& or C& reference).
Because ((B*)obj)->foo(); behaves by design like B* b = (B*)obj; b->foo() and calls C::foo. You may call the base's method explicitly like ((B*)obj)->B::foo();.
#include <iostream>
using namespace std;
class A{
public:
int foo(){ return 1; }
};
class B: public A{
public:
virtual int foo(){ return 2; }
};
class C: public B{
public:
int foo() override { return 3; }
};
int main() {
A* obj = new C;
cout << obj->foo();
cout << ((B*)obj)->B::foo();
cout << ((C*)obj)->foo();
return 0;
}
Output: 123
Member function foo is virtual from class B downwards, i.e. also in C, even if it is not marked virtual or override there.
Thus, call ((B*)obj)->foo() is a virtual call, actually resulting in calling C::foo.
class Base1
{
public:
virtual ~Base1(){}
virtual void whatever()
{
cout << "whatever" << endl;
}
};
class Base2
{
public:
virtual ~Base2(){}
virtual void aFunc(int i) = 0;
};
class A : public Base1, public Base2
{
public:
A()
{}
~A()
{}
virtual void aFunc(int i) final
{
cout << "func" << endl;
}
};
int main()
{
void* a;
a = new A();
(static_cast<Base2*>(a))->aFunc(0);
Base2* ptr = static_cast<Base2*>(a);
ptr->aFunc(0);
return 0;
}
This example prints out "whatever" instead of "func", if I change the line with void* to A* than it prints out "func". Is this a known behavior? I would expect that's the case just don't know why.
Is this a known behavior?
Yes. Behaviour is well-defined if you convert to void* and then back to the same type. It's undefined if you convert back to a different type.
I would expect that's the case just don't know why.
There's no guarantee that a base sub-object has the same address as the complete object; in fact, if there's more than one non-empty base class, then at least one sub-object will have to be at a different address. So a valid conversion from A* to Base2* probably needs to adjust the value of the pointer, not just reinterpret it as a different type. Conversion to void* and back can't make that adjustment.
I know this question must have been covered endless of times, but I've searched the previous questions, and nothing seems to pop.
It's about inheritance and virtual functions i C++. I have a problem with calling virtual functions in subclasses from the superclass.
Let me give an example. Start of with three classes, which inherit from each other.
class A {
void foo() { bar() }
virtual void bar() { }
};
class B : public A {
virtual void bar() { }
};
class C : public B {
virtual void bar() { // do something }
};
Now I wanna have a variable declared as B* but instantiated as C*.
B* myObject = new C();
myObject->foo();
When I do this, and call foo() on myObject, then A::foo() is calling bar(). But only B::bar() is called, not C::Bar() - which in reality myObject is, even though it's declared as B, which again affects that "// do nothing" doesn't get executed.
How do I tell A::foo(), that it needs to look at lowest implementation?
Makes sense?
// Trenskow
EDIT:
C::Foo is not the problem. Foo is being called in class A, as it's the only place it's implemented. The problem arises, when A:Foo calls Bar(). Then B:Bar is called and not C::Bar.
Maybe the problem is, that in my implementation, I only get a void* pointer to the object in A.
Like this:
void A:Foo(void *a) {
A* tmpA = static_cast<A*> (a);
tmpA->bar();
}
Now the compiler thinks, that tmpA is an A. But somehow it manages to figure that it's a B*, and calls B::Bar, when in fact tmpA is a C* and it should be calling C::Bar.
The following prints "A::foo C::bar" as expected. Are you getting something different? B::bar is never called because C is the actual runtime type of the object. In C::bar, you could call B::bar explicitly by adding B::bar(); to its body.
#include <iostream>
using namespace std;
class A {
public:
void foo() { cout << "A::foo "; bar(); }
virtual void bar() { }
};
class B : public A {
public:
virtual void bar() { cout << "B::bar" << endl; }
};
class C : public B {
public:
virtual void bar() { cout << "C::bar" << endl; }
};
int main()
{
B* c = new C();
c->foo();
return 0;
}
void A:Foo(void *a) {
A* tmpA = static_cast<A*> (a);
tmpA->bar();
}
This is undefined behaviour. You cannot cast a B* to a void*, then cast that void* back to an A*. If you want it to work properly, you have to ditch the void*. Alternatively, you could try dynamic_cast.
Assuming you mistyped your last block of code and the names match:
B* variable = new C();
variable->foo();
Then the C::Foo method is being called or you are using a terribly bad compiler.
(This also assumes that you don't actually have a compiler error in C::Foo, and that the comment is actually something like std::cout << "Hi mom!" << std::endl;)
Don't you mean:
B* myObject = new C();
myObject->foo(); // not variable->foo()
class A
{
public:
void foo() { bar(); }
virtual void bar() { std::cout << "A"; };
};
class B : public A
{
public:
virtual void bar() { std::cout << "B";};
};
class C : public B
{
public:
virtual void bar() { std::cout << "C"; }
};
This prints 'C' as expected.
I don't follow. You're saying
But only B::bar() is called, not
C::Bar()
No. You invoked the constructor of class C, which means that the vtable makes bar() point to C::bar(), so calling foo() in this case would go straight to C::bar().
If you want to force A::foo() to explicitly only call A's implementation, you can do that by just writing
void foo() { A::bar(); }
What exactly are you trying to do?
what compiler are you using? Visual Studio (IIRC) usually has the runtime type information turned off by default so maybe its just something simple as that?