简体   繁体   中英

Error in C++ with a Getter function

I'm getting a strange error when i tried do display some information. Here it's a portion of my class code:

class Vertex
{
public:
    Vertex(int name)
    {
        name = name;
        pred = NULL;
        color = 'w';
        desc = 1000000;
    }

    int getName() {return name;} 
    char getColor() {return color;} 
    Vertex* getPred() {return pred;} 
    int getDesc() {return desc;}

    void setColor(char c) {color = c;} 
    void setPred(Vertex *p) {pred = p;}
    void setDesc(int d) {desc = d;}

private:
    int name;
    char color;
    Vertex* pred;
    int desc;
};

I believe this is okay... but when i do this in my main function (which i believe it's also ok...):

for(int z = 1; z <= nsomething; z++){
    Vertex v(z);
    cout << "from z=" << z << " is vertex name: " << v.getName() << endl;
    cout << "from z=" << z << " is vertex color: " << v.getColor() << endl;
    Vertex *vp = &v;
    cout << "from z=" << z << " is vertex name: " << vp->getName() << endl;
    cout << "from z=" << z << " is vertex color: " << vp->getColor() << endl;
}

I get this:

from z=1 is vertex name: 1854338496
from z=1 is vertex color: w
from z=1 is vertex name: 1854338496
from z=1 is vertex color: w
from z=2 is vertex name: 1854338496
from z=2 is vertex color: w
from z=2 is vertex name: 1854338496
from z=2 is vertex color: w

Can someone help me, telling why is this happening only with the getName()?

Thanks in advance!

Change constructor

Vertex(int name)
{
    name = name;
    pred = NULL;
    color = 'w';
    desc = 1000000;
}

the following way

Vertex(int name)
{
    this->name = name;
    pred = NULL;
    color = 'w';
    desc = 1000000;
}

or

Vertex(int name)
{
    Vertex::name = name;
    pred = NULL;
    color = 'w';
    desc = 1000000;
}

or

Vertex(int name) : name( name )
{
    pred = NULL;
    color = 'w';
    desc = 1000000;
}

or

Vertex(int name) : name( name ), pred( NULL ), color( 'w' ), desc( 1000000 )
{
}

Otherwise local variable name that hides data member name of the class is assigned to itself.

Also it is better to declare all getters with qualifier const. For example

int getName() const {return name;} 
char getColor() const {return color;} 
Vertex* getPred() const {return pred;} 
int getDesc() const {return desc;}

The variable name in your constructor shadows the member name . You thus assign the local variable to itself.

To fix this, either write this->name = name; or change the name of the parameter.

You could also use the member initialization list instead, which IMO is the best way to do this. With that, be aware of the fact that members will always be initialized in the order they were declared in the class, regardless of the order of initializers in the list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM