8

MyFill is a class and MyFill2 is a function inside that class.

What is the difference in between declaring a variable inside public function of the class like this (thickness and lineType) -->

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 just declaring them in private label (private:), like 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 right. But the second one doesn't. If I want to go with the second option, what I need to do? A right code with some explanation might help.

sharkbait
  • 2,980
  • 16
  • 51
  • 89
ridctg
  • 135
  • 1
  • 2
  • 9
  • @umläute: I was getting an error: only static const integral data members can be initialized within a class – ridctg Jul 25 '13 at 09:06

6 Answers6

6

You are not allowed to assign values to variables inside the scope of the class, you can only do this inside of a 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 header needs to be changed to the above. You then set your values in any function you like, preferably your constructor like so:

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

Edit - To your question in the comments:

Identifiers for variable names in function parameters do not need to match between declaration and definition, only the types and their order need to match. It makes it more clear, but its not required.

A function prototype is really only seen as the following:

void MyFill2(Mat, Point);

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

void MyFill2(Mat m, Point p)
{
    //....
}
Joseph Pla
  • 1,600
  • 1
  • 10
  • 21
  • Isn't it contradictory what billz said above? "By placing thickness and lineType in class scope, they are member of class MyFill, and they are available in all MyFill objects." – ridctg Jul 25 '13 at 09:12
  • 1
    @ridctg those variables above in the constructor are still in class scope. They are just being initialised hence the :. They aren't local scope. I'm sorry I don't understand the question :( – Joseph Pla Jul 25 '13 at 09:14
  • Thnx. I need one more information. Every answer I got here used "MyFill(Mat img1, Point center1); void MyFill2 (Mat img, Point center);" But I am using "MyFill(Mat img1, Point center1); void MyFill2 (Mat img, Point center);" It still works. But I wanna know which one is correct and why. – ridctg Jul 25 '13 at 09:30
  • @ridctg One is a constructor and the other is just a function with the same arguments. They are both correct and there is absolutely nothing wrong with having them both in your class. – Joseph Pla Jul 25 '13 at 09:34
  • 1
    @ridctg I think i understand what you are saying now. Identifiers for variable names in function parameters do not need to match between declaration and definition, only the types and their order need to match. – Joseph Pla Jul 25 '13 at 09:37
  • Yes. That's what I wanted to know. It seems weird to me that I can use both similar and different variable names in the above context. Could you please give a little explanation? – ridctg Jul 25 '13 at 09:48
  • @JosephPla thanks so much for your thoughtful answer and explanation! Could you say what is better style? I reckon if the variable is only needed locally, it is better to just declare it locally, right? – Hirek Oct 29 '18 at 17:04
3

What is the difference in between declaring a variable inside public function of the class like this (thickness and lineType)

thickness and lineType are defined in function scope, it's called local variable of MyFill2 function.

void MyFill::MyFill2(Mat img, Point center)
{
    int thickness = -1; // thickness is a local variable in MyFill2,
                        // it's 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 thickness and lineType in class scope, they are member of class MyFill, 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 could to initialize class member in member initializer list

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

Hope this answers your question.

billz
  • 44,644
  • 9
  • 83
  • 100
2

You declare the member variables in the class definition, then in the constructor you use an initializer list to initialize the 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);
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

you cannot assign values in the class-declaration. you should do that 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) {
  // ...
}
umläute
  • 28,885
  • 9
  • 68
  • 122
1

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

In the latter, you declared them for the scope of the class, so you could reference them in any function of that class.

Note that your style of initialization does only work if you are using a C++11 conform compiler, with C++03 you would need to initialize them in your constructor:

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

and only declare them as

private:
    int thickness;
    int lineType;

If you only need these variables in that one function, go with the local variables.

nikolas
  • 8,707
  • 9
  • 50
  • 70
0

The c++11 allows the initialization of non-const and non-static members inside class declaration, you may refer to the discussions in this page

Community
  • 1
  • 1
yichucai
  • 156
  • 1
  • 3