Declaring variable inside functions of a class in C++

15,642

Solution 1

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)
{
    //....
}

Solution 2

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.

Solution 3

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);
}

Solution 4

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) {
  // ...
}

Solution 5

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.

Share:
15,642

Related videos on Youtube

ridctg
Author by

ridctg

Enthusiastic Software Developer. Passionate UX/UI Researcher. Experienced Telecom Professional

Updated on September 15, 2022

Comments

  • ridctg
    ridctg over 1 year

    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.

  • awesoon
    awesoon almost 11 years
    Note, that since C++11 you can initialize class members in class declaration.
  • awesoon
    awesoon almost 11 years
    Since C++11 initialization in class declaration is also possible.
  • ridctg
    ridctg almost 11 years
    Initializer list... I think that's what I need to use. Thanks @Joachim
  • ridctg
    ridctg almost 11 years
    @soon: I am using VS2010. Is that why I am getting the error?
  • ridctg
    ridctg almost 11 years
    Thanks. I think that's the answer. I am going to try it now.
  • ridctg
    ridctg almost 11 years
    Thanks umläute and soon!
  • ridctg
    ridctg almost 11 years
    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."
  • Joseph Pla
    Joseph Pla almost 11 years
    @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 :(
  • ridctg
    ridctg almost 11 years
    Thanks... This answer is common And I am going to try it
  • billz
    billz almost 11 years
    @ridctg No problem, glad it helped.
  • awesoon
    awesoon almost 11 years
    @ridctg, Maybe. I don't use MSVC, so, I can't tell you what C++11 features it supports.
  • ridctg
    ridctg almost 11 years
    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.
  • Joseph Pla
    Joseph Pla almost 11 years
    @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
    Joseph Pla almost 11 years
    @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.
  • ridctg
    ridctg almost 11 years
    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?
  • umläute
    umläute almost 11 years
    @ridctg great to be of help, but be aware that stackoverflow policy discourages thank you posts.
  • Hirek
    Hirek over 5 years
    @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?