Declaring a variable inside class functions in C ++

MyFill is a class, and MyFill2 is a function inside this class.

What is the difference between declaring a variable inside a public function of a class (thickness and line type) →

MyFill::MyFill (Mat img, Point center) { MyFill2 (img, center); } void MyFill::MyFill2(Mat img, Point center) { int thickness = -1; int lineType = 8; circle ( img, center, w/32, Scalar( 0, 0, 255 ), thickness, lineType ); } 

... and simply declaring them in a private label (private :), as in the header file →

 class MyFill { public: MyFill(Mat img1, Point center1); void MyFill2 (Mat img, Point center); private: int thickness = -1; int lineType = 8; }; 

The first one works correctly. But the second is not. If I want to go with the second option, what should I do? The right code with some explanation may help.

+7
c ++ variables class
source share
6 answers

You are not allowed to assign values ​​to variables within the scope of the class, you can only do this inside the function or in the global scope.

 class MyFill { public: MyFill(Mat img1, Point center1); void MyFill2 (Mat img, Point center); private: int thickness; int lineType; }; 

Your title should be changed to the previous one. Then you set your values ​​in any function that you like, preferably your constructor, for example:

 MyFill::MyFill(Mat img1, Point center1) { thickness = -1; lineType = 8; } 

Edit - To your question in the comments:

The identifiers of variable names in the functional parameters must not coincide between the declaration and the definition, only the types and their order must correspond. This makes it more understandable, but not required.

A function prototype is really only considered as:

 void MyFill2(Mat, Point); 

When you give it a definition, that is, when the purpose of the identifiers really matters:

 void MyFill2(Mat m, Point p) { //.... } 
+4
source share

You declare member variables in the class definition, and then in the constructor you use the list of initializers to initialize member variables:

 class MyFill { public: MyFill(Mat img1, Point center1); void MyFill2 (Mat img, Point center); private: // Just declare the member variables int thickness; int lineType; }; // ... MyFill::MyFill(Mat img1, Point center1) : thickness(-1), lineType(8) // Initializer list to initialize the member variables { MyFill2(img1, center1); } 
+2
source share

What is the difference between declaring a variable inside a public class function (thickness and line type)

Thickness

and the line type is defined in the function area , it is called the local variable of the MyFill2 function.

 void MyFill::MyFill2(Mat img, Point center) { int thickness = -1; // thickness is a local variable in MyFill2, // it destroyed when MyFill2 function goes out of scope // thickness is not accessable in any other member function of MyFill // or object. int lineType = 8; // same here } 

By placing the thickness and line type in the class area, they are members of the MyFill class, and they are available in all MyFill objects.

 class MyFill { private: int thickness = -1; // this is a C++11 form of initialize class member. // In C++03, you need to initialize them in constructor // thickness is a member of MyFill, it will exist in all life of MyFill object. int lineType = 8; }; 

In C ++ 03, you can initialize a class member in a list of member initializers

 MyFill::MyFill(Mat img1, Point center1) : thickness(0), lineType(0) // Initializer list to initialize the member variables { } 

Hope this answers your question.

+2
source share

you cannot assign values ​​in a class declaration. you must do this in the constructor of your class:

 class MyFill { public: MyFill(Mat img1, Point center1); private: int thickness ; int lineType; }; MyFill::MyFill(Mat img1, Point center1) : thickness(-1), lineType(8) { // ... } 
+1
source share

In the first case, you declare local variables that are valid only within the scope. Outside you cannot use them.

In the latter, you declared them for the scope of the class, so you can refer to them in any function of this class.

Please note that your initialization style only works if you use a compiler with C ++ 11 support, and with C ++ 03 you must initialize them in your constructor:

 MyFill::MyFill(Mat img1, Point center1) : thickness(-1), lineType(8) { /* ... */ } 

and declare them only as

 private: int thickness; int lineType; 

If you need only these variables in this one function, go to local variables.

+1
source share

C ++ 11 allows you to initialize non-constant and non-static members inside a class declaration, you can refer to the discussions on this page

0
source share

All Articles