I was reading this article and it states
This calls Derived::f( complex ). Why? Well, remember that
Derived doesn't declare "using Base:f;", and so clearly Base::f( int )
and Base::f( double ) can't be called.
I decided to try this out and used this code
class Base {
public:
virtual void f( int ) {
cout << "Base::f(int)" << endl;
}
virtual void f( double ) {
cout << "Base::f(double)" << endl;
}
virtual void g( int i = 10 ) {
cout << i << endl;
}
};
class Derived: public Base {
using Base::f;
public:
void f( complex<double> ) {
cout << "Derived::f(complex)" << endl;
}
void g( int i = 20 ) {
cout << "Derived::g() " << i << endl;
}
};
int main() {
Derived d;
d.f(1.0);
}
I get the error
main.cpp: In function 'int main()':
main.cpp:43:16: error: 'virtual void Base::f(double)' is inaccessible within this context
43 | d.f(1.0);
My question is how do I use using Base::f; and how do I fix this issue ?
Simple. You should write the 'using' declaration under public section of class as below. If you put the declaration in private section, Base functions are available to Derived class but they will become private members of Derived class. That is why your compiler is throwing 'inaccessible' error.
#include <iostream>
#include <complex>
using namespace std;
class Base {
public:
virtual void f( int ) {
cout << "Base::f(int)" << endl;
}
virtual void f( double ) {
cout << "Base::f(double)" << endl;
}
virtual void g( int i = 10 ) {
cout << i << endl;
}
};
class Derived: public Base {
public:
using Base::f;
void f( complex<double> ) {
cout << "Derived::f(complex)" << endl;
}
void g( int i = 20 ) {
cout << "Derived::g() " << i << endl;
}
};
int main() {
Derived d;
d.f(1.0);
}
Related
I have 3 classes, A,B, and C.
Edited code.
#include <iostream>
class A {
public:
virtual void print() {
std::cout << "A" << std::endl;
}
A() : x(0) {} // constructor
void SetX (int tmp){
x = tmp;
}
void printX() {
std::cout << "x = " << x << std::endl;
}
private:
int x;
};
class B : public A {
public:
virtual void print(){
std::cout << "B" << std::endl;
}
B(int tmp) : A() {
SetX(tmp);
}
};
class C : public B {
public:
virtual void print(){
std::cout << "C" << std::endl;
}
C(int tmp) : B(tmp) {
std::cout << "Debug print" << std::endl;
}
};
int main() {
B* b = new B(1);
C* c = new C(2);
b->print();
b->printX();
c->print();
c->printX();
return 0;
}
print for b->printX = 1
and for c->printX = 0
when creating object B everything is work just fine.
but when creating object C the default value of class A (var x) is still 0 (default value).
i added debug line inside the constructor of class C, but i didnt see it in logs, looks like the constructor is not running. i did the same thing in the constructor of class B when creating only object C and i didnt see any debug print too.
There no any compiling error for this code. build finish successfully.
The following is legal and working C++. Your code was not legal C++.
I added a void printX() method to class A to illustrate the example.
#include <iostream>
class A {
public:
virtual void print() {
std::cout << "A" << std::endl;
}
A() : x(0) {} // constructor
void SetX (int tmp){
x = tmp;
}
void printX() {
std::cout << "x = " << x << std::endl;
}
private:
int x;
};
class B : public A {
public:
virtual void print(){
std::cout << "B" << std::endl;
}
B(int tmp) : A() {
SetX(tmp);
}
};
class C : public B {
public:
virtual void print(){
std::cout << "C" << std::endl;
}
C(int tmp) : B(tmp) {
std::cout << "Debug print" << std::endl;
}
};
int main() {
B* b = new B(1);
C* c = new C(2);
b->print();
b->printX();
c->print();
c->printX();
return 0;
}
i am trying to write a very simple code as a practice. The problem is when i make friend a member function of one class to another, it says inaccesible but when i declare the whole class as friend of another class it works fine.
#include <iostream>
using namespace std;
class gpa2;
class gpa1 {
private:
int no1;
int no2;
public:
void setnum1(int n1, gpa2&xp) {
cout << " the friend member function is : " << xp.no4;
}
void setnum2(int n2) {
no2 = n2;
cout << "num2 is : " << no2 << endl;
};
};
class gpa2 {
private:
int no3;
int no4;
friend void gpa1::setnum1(int, gpa2&);
public:
void setnum3(int n3) {
no3 = n3;
cout << "num3 is : " << no3 << endl;
}
void getnum4(int n4) {
cout << "num4 is : " << n4 << endl;
}
};
int main() {
gpa1 g1;
gpa2 g2;
g1.setnum1(15, g2);
g1.setnum2(30);
g2.setnum3(45);
g2.getnum4(50);
return 0;
}
xp.no4 is not accessible, as setnum1 is a member function of gpa1, not gpa2
Implementing the function setnum1(int, gpa2&) is not necessary when defining it, and causing problems here: At the time of defining class gpa1 setnum1 can't be implemented, as class gpa2 is not yet defined. BUT implementing it after defining gpa2 is no problem at all.
Therefore, with some small changes: Godbolt example
#include <iostream>
using namespace std;
// forward declaration
class gpa2;
class gpa1 {
private:
int no1;
int no2;
public:
void setnum1(int n1, gpa2& xp);
void setnum2(int n2) {
no2 = n2;
cout << "num2 is : " << no2 << endl;
};
};
class gpa2 {
private:
int no3;
int no4;
friend void setnum1(int, gpa2&);
public:
void setnum3(int n3) {
no3 = n3;
cout << "num3 is : " << no3 << endl;
}
void getnum4(int n4) {
cout << "num4 is : " << n4 << endl;
}
int num4() { return no4; } // added accesibility to no4
};
// implementation of setnum1
void gpa1::setnum1(int n1, gpa2& xp) {
cout << " the friend member function is : " << xp.num4();
}
int main() {
gpa1 g1;
gpa2 g2;
g1.setnum1(15, g2);
g1.setnum2(30);
g2.setnum3(45);
g2.getnum4(50);
return 0;
}
In c++ in this hierarchy of classes
class n1
{
public:
virtual void tt() { cout << "n1" << endl; }
};
class n2:public n1
{
public:
void tt() { cout << "n2" << endl; }
};
class n3:public n2
{
void tt() { cout << "n3" << endl; }
};
int main()
{
n1 *k = new n3;
k->tt();
}
In the third class is tt overriding n1 virtual function or it is simply hiding the n2 implementation?
In C# i get that you can override at any level in the hierarchy the virtual method from the lowest class..but i dont know if it is the same in C++.
Class a
{
virtual void func();
};
class b : a
{
override func()
};
class c : b
{
override func()
};
You are overriding it.
If you aren't sure whether your override is correct, we have a keyword called override (c++11 required), which makes sure your override fits the virtual function / method declaration.
This should clear things up:
#include <iostream>
using namespace std;
class n1
{
public:
virtual void tt() { cout << "n1" << endl; }
};
class n2:public n1
{
public:
void tt() override { cout << "n2" << endl; }
};
class n3:public n2
{
void tt() override { cout << "n3" << endl; }
};
int main()
{
n1 *a = new n1;
n1 *b = new n2;
n1 *c = new n3;
a->tt();
b->tt();
c->tt();
delete a;
delete b;
delete c;
}
Outputs:
n1
n2
n3
Live
So in a 3 class hierarchy A->B->C if A has virtual method and B implements it ,it doesnt mean that classes derived from B will take the method already
If you do override it, then that override will be used.
If you don't override the method, then the last overriden method will be used.
class n1
{
public:
virtual void tt() { cout << "n1" << endl; }
};
class n2:public n1
{
public:
void tt() override { cout << "n2" << endl; }
};
class n3:public n2
{
};
Outputs:
n1
n2
n2
I am trying to understand how decorator pattern works and how much I can "stretch" it to me needs. Following this example, I have extended classes XYZ. There exist derived classes "KLM" (from XYZ)
Specifically, even though I have a decorator pattern, the derived decorator classes "KLM" have some functionality that does not show up in any of their base classes "XYZ", "D", "I" or "A".
So while normally I would instantiate an object as
I * inKLM = new L( new M( new K( new A )));
This would not allow me to access the K::doVirtR() , L::doVirtS() and M::doVirtT() functions (see code below). To access these I would need to downcast the inKLM pointer using dynamic_cast to each of classes "KLM".
The problem is that I only manage to do this for the leftmost new in the expression above. I have read that polymorphism needs to be maintained in order for the dynamic casting to work, so I have tried to have a virtual destructor in all functions. Still I cannot get the dynamic cast to work for anything other than the "outer" new operation (in this case object of class "L").
Please see this code. How can I make not only "LinKLM" , but also "MinKLM" and "KinKLM" success in dynamic_casting ?
#include <iostream>
#include <list>
using namespace std;
class D; //decorator base
struct I { //interface (for both Base and DecoratorBase
I(){
cout << "\n| I::ctor ";
}
virtual ~I(){
cout << "I::dtor |" ;
}
virtual void do_it() = 0;
virtual void regDecorator(D* decorator) = 0;
virtual void train() = 0;
virtual void et() = 0;
};
class D: public I { //DecoratorBase : has same-named fns as Base (must be exported on I) and calls upon them.
public:
D(I * inner) : m_wrappee(inner) {
cout << "D::ctor ";
regDecorator(this);
}
virtual ~D() {
cout << "D::dtor ";
delete m_wrappee;
}
void do_it() {
m_wrappee->do_it();
}
virtual void et() {
cout << "filling in for lack of et() in derived class\n";
} //almost pure virtual, just not implemented in all derived classes
void train(){
m_wrappee->train();
}
private:
void regDecorator(D* decorator){
m_wrappee->regDecorator(decorator);
}
I * m_wrappee;
};
class A: public I { //Base has all the basic functionality
public:
A() {
cout << "A::ctor " ;
decList.clear();
}
~A() {
cout << "A::dtor |" ;
}
void do_it() {
cout << 'A';
}
void train(){
et();
}
void regDecorator(D* decorator)
{
if (decorator) {
cout << "reg=" << decorator << " ";
decList.push_back(decorator);
}
else
cout << "dec is null!" <<endl;
}
private:
void et()
{
//size_t counter=0;
list<D*>::iterator it;
for( it=decList.begin(); it != decList.end(); it++ )
{
//if ( (*it)->et() )
(*it)->et();
//else
// cout << "couldnt et cnt=" << counter << endl;
//counter++;
}
}
std::list<D*> decList;
};
class X: public D { //DerivedDecoratorX ..
public:
X(I *core): D(core){
cout << "X::ctor ";
}
virtual ~X() {
cout << "X::dtor ";
}
void do_it() {
D::do_it();
cout << 'X';
}
void doX() {
cout << "doX" << endl;
}
protected:
virtual void doVirtR() = 0;
private:
void et(){
cout << "X::et" <<endl;
}
};
class K: public X {
public:
K(I * core):X(core) {
cout << "K::ctor " ;
}
virtual ~K() {
cout << "K::dtor ";
}
void doVirtR(){
cout << "doVirtK" <<endl;
}
};
class Y: public D {
public:
Y(I *core): D(core){
cout << "Y::ctor ";
}
virtual ~Y() {
cout << "Y::dtor ";
}
/*void et(){
cout << "Y::et" <<endl;
}*/
void do_it() {
D::do_it();
cout << 'Y';
}
void doY() {
cout << "doY" << endl;
}
protected:
virtual void doVirtS() = 0;
};
class L: public Y{
public:
L(I * core):Y(core) {
cout << "L::ctor ";
}
virtual ~L() {
cout << "L::dtor ";
}
void doVirtS(){
cout << "doVirtL" <<endl;
}
};
class Z: public D {
public:
Z(I *core): D(core){
cout << "Z::ctor ";
}
virtual ~Z() {
cout << "Z::dtor ";
}
void et(){
cout << "Z::et" <<endl;
}
void do_it() {
D::do_it();
cout << 'Z';
}
void doZ() {
cout << "doZ" << endl;
}
virtual void doVirtT() = 0;
};
class M: public Z{
public:
M(I * core):Z(core) { //must add D(core) here explicitly because of virtual inheritance in M's base class (Z).
cout << "M::ctor " ;
}
virtual ~M() {
cout << "M::dtor ";
}
void doVirtT(){
cout << "doVirtM" <<endl;
}
};
int main(void) //testing dynamic casting
{
I * inKLM = new L( new M( new K( new A )));
L * LinKLM = dynamic_cast<L *>( inKLM);
M * MinKLM = dynamic_cast<M *>( inKLM);
K * KinKLM = dynamic_cast<K *>( inKLM);
cout << endl;
if ( ! MinKLM ) cout << "null MinKLM!" << endl;
if ( ! LinKLM ) cout << "null LinKLM!" << endl;
if ( ! KinKLM ) cout << "null KinKLM!" << endl;
//KinKLM->doVirtR();
//LinKLM->doVirtS();
//MinKLM->doVirtT();
//LinKLM->D::train();
//KinKLM->do_it();
//MinKLM->doZ();
delete inKLM;
cout << endl;
return 0;
}
If you need access to functionality that is unique in some of the inner classes, you may be better off (depending on the particular problem) trying mixin classes. The basic idea is to have a template class inherit its template parameter. I have simplified the classes below but the principle is clear:
#include <iostream>
// your base class
class I {
public:
virtual void do_it() {}
};
// a decorator
template <class Base>
class T1 : public Base {
public:
void do_it() {
std::cout << "T1" << std::endl;
Base::do_it();
}
void unique_in_T1() {
std::cout << "Unique in T1" << std::endl;
}
};
// another decorator
template <class Base>
class T2 : public Base {
public:
void do_it() {
std::cout << "T2" << std::endl;
Base::do_it();
}
void unique_in_T2() {
std::cout << "Unique in T2" << std::endl;
}
};
// yet another decorator
template <class Base>
class T3 : public Base {
public:
void do_it() {
std::cout << "T3" << std::endl;
Base::do_it();
}
void unique_in_T3() {
std::cout << "Unique in T3" << std::endl;
}
};
int main(int argc, const char * argv[]) {
T3<T2<T1<I>>> my_object1;
my_object1.do_it();
my_object1.unique_in_T2();
T1<T3<I>> my_object2;
my_object2.do_it();
my_object2.unique_in_T3();
return 0;
}
Your class D is not needed anymore. The main purpose of that class is to wrap the object that actually does the job while maintaining the interface of I. With mixin classes there is no wrapping anymore as it has been replaced by inheritance, hence there is no need for the D class.
Here is a link to read more.
I want to know know how does g++ compiler knows which table to use if their are multiple vtable present in a base class. Like the following example.
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
class sample1
{
private:
int b;
public:
sample1():b(34)
{
cout << "In sample1 constructor" << endl;
}
virtual void print_b()
{
cout << this->b << endl;
}
void print_all()
{
this->print_b();
}
void sample_print_()
{
//cout << this->a << "String : " << this->str1 << endl;
cout << "hello" << endl;
this->print_all();
}
};
class sample2
{
private:
int b1;
public:
sample2():b1(34)
{
cout << "In sample1 constructor" << endl;
}
virtual void print_b1()
{
cout << this->b1 << endl;
}
void print_all1()
{
this->print_b1();
}
};
class sample : public sample1 , public sample2
{
private:
int a;
char *str1;
public:
sample():a(12),sample1()
{
strcpy(this->str1,"hello world");
cout << "In Constructor" << endl;
}
~sample()
{
free(this->str1);
cout << "In Destructor" << endl;
}
void sample_print()
{
//cout << this->a << "String : " << this->str1 << endl;
cout << "hello" << endl;
this->print_all();
}
virtual void print_a()
{
cout << this->a <<endl;
}
};
In above example, child class sample has two parent classes sample1 and sample2 and each of these class have vtable of their own. What if i call a virtual function from sample(child class)? How does the compiler know, in which class that virtual function is present so that it call use that particular vtable pointer ? I know their will be two vtable pointer present in sample(child class) class , so how does compiler know which one to use ?