Function Declaration Inside or Outside the Class

What is the benefit of declaring a function outside class in c++?

Defining a complex member function in a class makes the class definition too complicated and not well-readable.

Bear in mind that you can declare a member function with the function specifier inline without its definition within the class.

Take into account that if the compiler will make non-inline calls of a member function of a class defined within the class nevertheless all other requirements to the function will be the same as for an inline function. For example the function definition with the class definition can be included in many compilation units.

Defining a member function outside a class allows to separate the interface and its realization.

Is there any difference between declaring a function inside our outside the component's body?

In the case of React Native, declaring a function outside of the class is like making a static function. If you place the function in the class, it will be created for each instance of the class which in this case would be unnecessary.

In your example, I would consider whether foo() should only be used within that file or within other files as well. In the case of the latter, make a separate folder called utils/, put the function in there and import it where you need it.

When it comes to props, you could create the function outside of the class and invoke it with call() and would then have access to props:

foo.call(this);

const foo = () => console.log(this.props);

Defining JavaScript functions inside vs. outside of a class

From the MDN doc:

JavaScript classes, introduced in ECMAScript 2015, are primarily syntactical sugar over JavaScript's existing prototype-based inheritance. The class syntax does not introduce a new object-oriented inheritance model to JavaScript.

So this...

class TestClass {
constructor(myName) {
this.name = myName;
}

getName() {
return this.name;
}

static getName2() {
return 'getName2 result';
}
}

...is exactly equivalent to this:

const TestClass = function(myName) {
this.name = myName;
}

TestClass.prototype.getName = function() {
return this.name;
}

TestClass.getName2 = function() {
return 'getName2 result';
}

So whether you use the older prototype syntax or the newer ES6 class syntax is just a matter of personal preference, and as you suspected, defining methods directly on a class is exactly equivalent to creating a static class method.

Where do you typically declare and define functions outside a class in c++?

Yes, you can have the main cpp file along with separate header/source files for your own functions. Something like this:

/main.cpp

#include "myFunctions.h"

int main(){

printSum(5, 5);
return 0;
}

/myFunctions.h

#ifndef MYFUNCTIONS_H
#define MYFUNCTIONS_H

void printSum(int, int);

#endif

/myFunctions.cpp

#include "myFunctions.h"

void printSum(int num1, int num2){

cout << num1 + num2 << "\n"

}

Friend function inside class and outside class, what difference does it make?

The problem is, that you are accessing private members of your class (dd,mm,yy), what is only allowed for functions of that class or friends. So you have to declare the function a friend inside of the class and than it can be implemented outside of the class.

class time
{
private:
int dd,mm,yy;
public:
friend istream & operator >>(istream &ip,time &t); // declare function as friend to allow private memeber access
friend ostream & operator <<(ostream &op,time &t); // declare function as friend to allow private memeber access

void validate();
};

Now you can write the implementation outside of the class and access private variables.

istream & operator >>(istream &ip,time &t)
{
cout<<"\nEnter Date";
ip>>t.dd;
cout<<"\nEnter Month";
ip>>t.mm;
cout<<"\nEnter Year";
ip>>t.yy;
return ip;
}

ostream & operator <<(ostream &op,time &t)
{
op<<t.dd<<"/"<<t.mm<<"/"<<t.yy;
return op;
}

Is it possible to define a function outside a class using a private return type in C++?

It's possible. Because the return type comes before the name of the function---so the compiler doesn't yet know that you're defining a member of BST<T>---you have to specifically tell it that Node is a member of that class. You must use a qualified name:

template<typename T>
typename BST<T>::Node* BST<T>::searchNode(Node* node, const T& data) const {
//Function Declaration, somewhat long
}

(In C++20, typename can be omitted.)

You can also use a trailing return type to avoid the extra qualification:

template<typename T>
auto BST<T>::searchNode(Node* node, const T& data) const -> Node* {
//Function Declaration, somewhat long
}

Here, because the compiler has already seen that the function is a member of BST<T>, it will look up Node* in that class.

The fact that Node is private does not matter here. The private name Node is being "used" by the member function definition, which is part of the class, so access is allowed.



Related Topics



Leave a reply



Submit