This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Why is this not allowed in C++?
Why is this not allowed in C++...??
class base
{
private:
public:
void func()
{
cout<<"base";
}
};
class derived : private base
{
private:
public:
void func()
{
cout<<"derived";
}
};
int main()
{
base * ptr;
ptr = new derived;
((derived *)ptr)->func();
return 0;
}
I am getting an error
**61 C:\Dev-Cpp\My Projects\pointertest.cpp `base' is an inaccessible base of `derived'**
My question is that since func() is defined public in derived class and the statement
((derived *)ptr)->func(); is trying to display the func() of derived..Why is there an accessible issue due to mode of inheritance..How does mode of inheritance(private) affects the call although I already have public derived func() in derived class..?
If mode of inheritance is changed to public I get my desired result..But a case where func() is private in base(so as func() of base is not inherited) and also func() is public in derived and mode of inheritance is public why still am I getting my desired result..Shouldn I be getting an Compile error as in the previous case ??
I am totally confused ..Please tell me how the compiler works in this case..??
You can't let the base pointer point to the derived object when there is private inheritance.
Public inheritance expresses an isa relationship. Private inheritance on the other hand expresses a implemented in terms of relationship
Ther compile error refers to the line:
ptr = new derived;
Related
This question already has answers here:
Why does an overridden function in the derived class hide other overloads of the base class?
(4 answers)
Closed 5 years ago.
I'm normally a C# guy, and it handles this fine, largely under its handling of "best match" resolving the method to call. I'm trying to do something similar in C++ now, but am getting a compile error. Long story short, it's a combination of method overloading and overriding.
class Bar : public Foo { } //Contents don't really matter here
class Base
{
public:
virtual void Do(Foo* foo) { }
virtual void Do(Bar* bar) { }
};
class Derived : public Base
{
public:
virtual void Do(Bar* bar) { }
}
Foo* foo = new Foo();
Derived* d = new Derived();
d->Do(foo); //cannot initialize a parameter of type 'Bar*' with an rvalue of type 'Foo*'
So, it tries to resolve the method against only the methods in Derived rather than recognizing the base class implements a valid match. Again, C# finds the base method. Can C++ not do this, or am I missing something?
A function in a derived class hides any functions of the same name in a base class (even if you're overriding a virtual function). To unhide the other overloads, use a "using" statement:
class Derived : public Base
{
public:
virtual void Do(Bar* bar) { }
using Base::Do;
};
This question already has answers here:
Does every object of virtual class have a pointer to vtable?
(9 answers)
Closed 6 years ago.
I've read a lot of posts and everyone says, that virtual table is per class, not per object and object only has _vtpr pointer to shared vtable. But please consider this example:
class Base
{
public:
virtual void func1(void) {}
virtual void func2(void) {}
private:
int dummy;
};
class Der1 : public Base
{
public:
virtual void func1(void) {}
private:
int dummy;
};
class Der2 : public Base
{
public:
virtual void func2(void) {}
private:
int dummy;
};
int main(void)
{
Base * obj1 = new Der1;
Base * obj2 = new Der2;
}
Does obj1 and obj2 relates to that one unique Base class vtable? I believe the answer is no, but can you please explain? And if both of these objects do relate to the same vtable, how it is determined which methods to be called? For example, obj1->func1 reference is different than obj2->func1.
UPDATE:
What operations are executed when doing Base * obj1 = new Der1;? Can someone write a pseudo code for these actions?
I hope the following illustration will help. Derived classes just copy the virtual table of base class, but they may modify the appropriate entry. The red part shows that given class has modified the entry in v-table. So, when instance of derived class is created, the modified table (specific to that class) is taken into consideration.
It is up to the compiler how duplicity of base class is handled (if base class has large number of functions - would derived have copy of entire virtual function table, and would modify the entries?).
IMO, compiler will simply copy entire table for each derived class to keep things simple. Searching appropriate method to call then becomes simple.
This question already has answers here:
Pure virtual destructor in C++
(2 answers)
Closed 8 years ago.
I am using Qt to develop an applicatio for which I am seeing a segmentation fault in my destructor of my virtual base class on exiting the application. I think it's to do with declaring the member variable static, but I'm not certain. Any pointers on what's going on would help. Below is my sample code. I have removed all the member functions for clarity.
In header file:
class Base : public QObject
{
public:
Base() {}
virtual ~Base() = 0; /// Fault occurs here in the debugger
};
class Child1: public Base
{
public:
Child1() {}
~Child1() {}
};
class Service
{
public:
Service() {}
~Service() {}
private:
static Child1 m_base;
};
In source file:
Child1 Service::m_base;
When I exit the application I get a segmentation fault in the Base class destructor. Is it because m_base static member variable does not exist at the time the destructor is called, but it's virtual!
BTW, I got rid of the problem by making m_base a pointer to Base class and instantiating it in definition, but I'd still like to know what's wrong with the code above.
Thanks!
Your sample code is incorrect, because you cannot create an instance to Base, since it is abstract.
Please be more specific.
EDIT:
I'm still not sure how this compiles, but You will have to add the Base destructor implementation:
Base::~Base()
{
}
This question already has answers here:
What is the practical use of protected inheritance?
(2 answers)
Closed 8 years ago.
I'm trying to experiments with inheritance in c++. I've written the following code:
#include <stdio.h>
class A
{
public:
virtual void foo();
};
class B: A
{
void foo();
};
void B::foo()
{
printf("Derived class");
}
void A::foo()
{
printf("Base class");
}
int main()
{
A *a= new B();
a->foo();
}
But I've an error described as
test.cpp: In function ‘int main()’: test.cpp:26:14: error: ‘A’ is an
inaccessible base of ‘B’
It works fine if I replace the line class B: A to class B: public A. But using this fact I really don't understand in what case private and protected inheritance may be needed. It's useless for me now.
You are accessing B::foo() using A*. But both the inherited A::foo() and overridden B::foo() are private.
About inheritance concept:
public : derived class IS-A base class
private/protected: derived class has a base class \ derived class is implemented-in-terms of base class.
Inherit publicly when derived object is replaceable to base object. Inherit privately when derived class only needs the implementation of base class (not interface). But in the later case Composition is preferred approach than inheritance.
check this and this
This question already has answers here:
multiple inheritance: unexpected result after cast from void * to 2nd base class
(3 answers)
Closed 8 years ago.
I have some codes like this:
#include <iostream>
using namespace std;
//base class
class Base
{
public:
Base(){}
virtual void f()
{
cout<<"father.f()"<<endl;
}
virtual ~Base(){}
};
//a empty class
class Base1
{
public:
Base1(){}
virtual ~Base1()
{
cout<<"~base1"<<endl;
}
};
//the child class of the base class
class Child :public Base
{
public:
Child(){}
virtual ~Child(){}
};
//the grandchild class of the base class,and child class of the base1 class
class Grand :public Base1,public Child
{
public:
Grand(){}
virtual ~Grand(){}
//overwrite base's f()
void f()
{
cout<<"grand.f"<<endl;
}
};
int main()
{
void *v = new Grand();
Base *b = (Base*)(v);
//i think it print "father.f()",but it print"~base1"
b->f();
return 0;
}
I run these code in code::blocks(version13.12),but it print "~base1",I don't konw why it don't print "father.f()"。I think the v point to the address of the grand's virtual table,so when i convert v to base and invoke the f function,then b invoke the first virtual function in the virtual table.
I do not know i'm right or wrong,can u tell me.Thank all of!
Because with multiple inheritance you can't cast directly to a upper class without passing by the child class. This because a void* doesn't contain any information about the layout in memory of the various classes that form your final Grand* instance.
To fix this you need indeed to cast it to it's runtime type:
Base *b = (Base*)(Grand*)v;
b->f();
Think about the fact that the layout could have Base in a different position. Think about another
class Grand2 : public Something, public Else, public Base1, public Child {
..
}
Now, where is Base located inside the Grand2 structure? In a different position compared to Grand. How could the compiler know the correct offset starting from just a pointer to a non specified class (which could be a Grand or a Grand2)?