In case of nested class, how do I access the "Inner" or "Child" class's member function?.
For example, the code, where I created "obj1". Now how do I access the "childPrint()" with"obj1"?
example_code:
#include<iostream>
using namespace std;
/////////////////////////////////////////////////
class Parent
{
public:
void print()
{
cout<<"this is from Parent"<<endl;
}
class Child
{
public:
void childPrint()
{
cout<<"this is from Child"<<endl;
}
};
};
int main()
{
Parent obj1;
obj1.print();
Parent::Child obj2;
obj2.childPrint();
obj1.Child::childPrint();///ERROR
return 0;
}
Now how do I access the "childPrint()" with"obj1"?
You can't.
obj1 is of type Parent, which doesn't contain any method called childPrint. An instance of Parent doesn't automatically contain an instance of Child (I think Java does something like this), they are still seperate classes. You can only call this method on an instance of Child.
obj1.Child::childPrint();
In this particular line, you need to understand that childPrint() is an instance member function of class Child. So, only an instance of class Child can call childPrint() function.
If childPrint() function is a static member function(class function) of class Child then it is possible to call it without creating any instance and no error will be shown in Parent::Child::childPrint();.
If you have a static method, then you can call it with:
/////////////////////////////////////////////////
class Parent
{
public:
...
class Child
{
public:
static void childPrint() { cout<<"this is from Child"<<endl; };
}
}
...
Parent::Child::childPrint();
These are separate classes without any automatic instanciation of child classes and vice versa.
Related
Let me first say this is a purely academic question, since what I want to do can be better accomplished with multiple layers of inheritance.
That said, I wonder if it's possible to override a virtual function with an existing function without writing a wrapper or adding any inheritance layers. Code:
int myfunc2() { return 2; }
class Parent {
public:
virtual int myfunc() { return 0; }
};
class Child1 : public Parent {
public:
int myfunc() override { return 1; }
};
class Child2 : public Parent {
public:
// There a way to do this?
// int myfunc() = myfunc2;
// instead of this?
int myfunc() { return myfunc2(); };
};
int main() {
Child2 baz;
return baz.myfunc();
}
I'd like to override myfunc in the definition of Child2 by simply "forwarding" the declaration to the existing declaration of myfunc2.
Is this, or something akin to it, possible?
Context: I've got a bunch of child classes, some of which have identical definitions of myfunc, some of which do not. A better solution is to create an intermediate child class that defines the common myfunc and have the pertinent children inherit from that instead.
// There a way to do this?
// int myfunc() = myfunc2;
// instead of this?
int myfunc() { return myfunc2(); };
No, there isn't.
There is a problem.
A non-static member function accepts one more implicit parameter: pointer to the object itself. While a stand-alone function does not have such a parameter, For example you may not use the keyword this or member access syntax inside the definition of a stand alone function.
What is the syntax for calling a method in an owning object from an owned object in c++?
Parent class:
class Parent
{
private:
Child child;
public:
Parent()
{
addChild(child);
}
void MethodToBeCalled(int someArgument)
{
}
};
Child class:
class Child
{
private:
void myVoid()
{
//Call parent method somehow
}
public:
Child()
{
}
};
I tried to make my question as simple and generic as possible (to be of benefit to as many as possible). Let me know if I can make it even clearer.
Thank you!
Here's a example. I had to modify your code a bit to get it compile:
class Component {};
class Parent;
class Child : public Component
{
private:
inline void myVoid();
Parent &parent_ref;
public:
Child(Parent &pr) : parent_ref{pr} {}
};
class Parent : public Component {
private:
Child child;
public:
Parent() : child{*this}
{
// addChildComponent(child);
}
void MethodToBeCalled(int someArgument)
{
}
};
inline void Child::myVoid()
{
parent_ref.MethodToBeCalled(1);
}
If you are sure that your Child object is a member subobject of Parent class (as in your example), you can use the container_of trick (see Understanding container_of macro in the Linux kernel)
class Child
{
private:
void myVoid();
};
class Parent
{
public:
Child child;
void MethodToBeCalled(int someArgument)
{
}
};
void Child::myVoid()
{
container_of(this, Parent, child)->MethodToBeCalled(42);
}
Obviously, tricks like this immediately restrict the usability of your Child class to always being a member of Parent class (at least when you intend to call myVoid() method on it).
A much better idea would be to just pass a reference to parent object to child object.
In your case, you can't, because Child and Parent have nothing in common except that they both inherit Component. Child has to have a Parent object in order to call MethodToBeCalled().
Suppose we have the following:
class Parent {
public:
virtual void run() {
for (int i = 0 ; i < bar.size() ; ++it)
cout << i << "\n" ;
};
protected:
static vector<int> foo() {return vector r({1,2,3,4,5});};
static vector<int> bar;
}
vector<int> Parent::bar = Parent::foo();
Now if I create a child class whose run function would be called externally, how can I redefine the foo function to return something else while still using the parent run function?
Edit: Sorry let me add some more information. Suppose the virtual function run() is a lot of code, all of which is essentially the same. The only difference in the parent and child classes is what values I want specified in the vector bar, so it would seem to be a little wasteful to redefine the virtual function in the child class. However, if you redefine Child::bar, and call Child::run(), the Parent::bar is used since it's defined in the parent class. Is there some way to have the line "vector Parent::bar = Parent::foo();" know in the Child class to use "Child::foo();"?
As usual. Override base virtual function in derived class.
class Parent {
public:
virtual bool run() {return bar;};
static bool foo() {return true;};
static bool bar;
};
class Child: public Parent
{
public:
static bool foo() { return false;};
};
You can then still use base version applying Base:: scope resolution:
int main() {
bool bc = Child::foo();
bool bp = Parent::foo();
std::cout << bc << bp;
return 0;
}
http://ideone.com/TdaNQ5
I am not sure what do you exactly want here. However, you can override the static function like this,
class Child: public Parent
{
public:
static bool foo() {return false;};
};
You really don't say much about your problem, therefore it is hard to distinguish what you need from what is potentially an XY problem.
One potential problem with your architecture is that you have a Parent and a Child classes that share a static variable bar yet you seem to initialize them differently for Parent and Child classes. However, there is only one bar that is shared both by Parent and Child objects, independently of who wrote to it last.
So, what do you expect when both a Parent and a Child objects are used simultaneously? The answer you look for depends on your answer on that one. In particular, if your answer is 'it is not designed to have Parent and Child objects operate simultaneously', then both classes should definitely not share a static variable.
I am trying to use a method from a child that class that inherits from a interface. The call to that method done from a client class (main method).
//interface method
class InterfaceMethod{
virtual void method(void) = 0;
}
This is the class that is inheritance the interface:
class ChildClass: public InterfaceMethod
{
public:
ChildClass(){}
~ ChildClass(){}
//virtual method implementation
void method(void)
{
//do stuff
}
//and this is the method that i want to use!
//a method declared in the child class
void anotherMethod()
{
//Do another stuff
}
private:
}
This is the client:
int main()
{
InterfaceMethod * aClient= new ChildClass;
//tryng to access the child method
(ChildClass)(*aClient).anotherMethod();//This is nor allowed by the compiler!!!
}
You want dynamic cast.
ChildClass * child = dynamic_cast<ChildClass*>(aClient);
if(child){ //cast sucess
child->anotherMethod();
}else{
//cast failed, wrong type
}
Try it like this:
static_cast<ChildClass*>(aClient)->anotherMethod();
You shouldn't do this unless you can be sure that you have an instance of the derived class.
is it possible in c++ to call a child function from a parent function.
Let's take an example: The parent class defines in a function (parse) the general workflow. The workflow then calls different methods which represent part of the flow (parseElementA). These functions can be overwritten by the child class, if not the standart function, which is part of the parent shall be used.
My issue is: I create a child object and execute the workflow function (parse). When the overwritten function (parseElementA) is called within the workflow function it calls the function from the parent and not from the child.
What could i do so it calls the overwritten function in child.
class Parent {
public:
void parse() { parseElementA(); }
virtual void parseElementA() { printf("parent\n"); }
};
class Child : public Parent {
public:
void parseElementA() { printf("child\n"); }
};
Child child;
child.parse();
the output is parent. What can I do that it returns child.
Thank you very much for any advice.
After fixing compiler errors from your code, it works fine.
#include <cstdio>
class Parent {
public:
void parse() { parseElementA(); }
virtual void parseElementA() { printf("parent\n"); }
};
class Child : public Parent {
public:
void parseElementA() { printf("child\n"); }
};
int main() {
Child child;
child.parse();
return 0;
}