How to call a member function of a class recursively due to its own definition in C ++?
I am new to C ++ and I need a member function of a class to call itself from its own definition, like this:
class MyClass {
public: // or private: ?
// Some code here
// ...
void myfunction();
// ...
};
void MyClass::myfunction()
{
// Some code here
// ...
// Call MyClass::myfunction() here, but how?
// ...
}
but I don't know the correct syntax for it and how can it be called by itself without usually creating such an object - object_name.member_function()
?
And would there be any difference if it myfunction()
belongs to public:
or private:
?
source to share
myfunction()
is in the scope of the class, so you can just call it:
class MyClass {
public:
// Some code here
// ...
void myfunction();
// ...
};
void MyClass::myfunction()
{
myfunction();
}
Note, however, that this will result in a stack overflow. You need a facility to stop the recursion.
source to share
but I don't know the correct syntax for it and how can it be called by itself without usually creating such an object -
object_name.member_function()
?
Using:
void MyClass::myfunction()
{
// Some code here
// ...
// Call MyClass::myfunction() here, but how?
// One way to call the function again.
this->myfunction();
// ...
}
this->mufunction()
can be replaced with myfunction()
. Usage this
is a stylistic option that makes the code easier to read for some, like me.
And would there be any difference if it
myfunction()
belongs topublic:
orprivate:
?
No, it will not. You can call any member function of a class from another member function.
source to share
Member functions are actually a form of syntactic sugar. They describe a function that somehow secretly takes a pointer to an instance of an object, which inside the function is available as this
.
struct Foo {
vod bar();
};
Foo foo;
foo.bar();
What you are actually doing in the call here is the call Foo::bar(&foo);
and bar
which actually takes a pointer Foo* this
. How this is done, from implementation to implementation, some compilers / architectures will use a special register to keep track of the current object.
An extra piece of syntactic sugar makes all member variables and functions visible to you within the member function as if they were locally localized
struct Foo {
int i;
int add(int n) {
return i + n;
}
int addx2(int n) {
return add(n) * 2;
}
};
What's really going on here:
return this->i + n;
and
return this->add(n) * 2;
This means that it is very easy to run it in a situation where you have conflicts between local and member names.
struct Foo {
int i;
Foo(int i) {
i = i; // not what you expected
}
};
For this reason, many engineers carefully use case or prefixes or suffixes to help them distinguish between members, parameters, and variables.
struct Foo { // Uppercase for types and functions
int m_i; // m_ for member
Foo(int i_, int j_) {
int i = sqrt(i));
m_i = i + j_;
}
int Add(int i) {
return i_ + i;
}
};
There are different patterns used by people - some people use _name
to refer to a member of some use name_
, and fn_
to refer to the members.
struct Foo {
int i_;
int add_(int _i) {
return i_ + _i;
}
};
The main thing is to be consistent.
source to share