Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem with return value of private variable within class

Problem with return value of private variable within class

Scheduled Pinned Locked Moved C / C++ / MFC
linuxperformancehelpquestion
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Michael Randolph
    wrote on last edited by
    #1

    I am compiling with g++ 4.2.4 on Ubuntu Hardy. I am trying to creating a public function to return a value of a private variable within the class. The output I am getting is not correct. What did I miss here?

    #include <iostream>

    using namespace std;

    class Point
    {
    public:
    // constructors
    Point(float f_x = 1.0, float f_y = 1.0, float f_z = 1.0);

    // destructors (Only necessary for new dynamic memory created)
    ~Point();
    
    float getX();
    

    private:
    float x, y, z;

    protected:

    };

    // define constructor with args
    Point::Point(float f_x, float f_y, float f_z)
    {
    cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl;
    }

    // define destructor
    Point::~Point()
    {
    cout << "Destructing now!!! " << (int)this /*unique identifier*/ << endl;
    }

    // public function to return private value of variable x
    float Point::getX()
    {
    return x;
    }

    int main()
    {
    // create instance of class
    Point myLocation(23, 54, 32);

    // create another instance of class
    //Point anotherLocation;
    
    // calling constructor with args
    //Point thirdLocation(23, 14, 52);
    
    // return value of x
    cout << myLocation.getX() << endl;
    
    return 0;
    

    }

    M S 2 Replies Last reply
    0
    • M Michael Randolph

      I am compiling with g++ 4.2.4 on Ubuntu Hardy. I am trying to creating a public function to return a value of a private variable within the class. The output I am getting is not correct. What did I miss here?

      #include <iostream>

      using namespace std;

      class Point
      {
      public:
      // constructors
      Point(float f_x = 1.0, float f_y = 1.0, float f_z = 1.0);

      // destructors (Only necessary for new dynamic memory created)
      ~Point();
      
      float getX();
      

      private:
      float x, y, z;

      protected:

      };

      // define constructor with args
      Point::Point(float f_x, float f_y, float f_z)
      {
      cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl;
      }

      // define destructor
      Point::~Point()
      {
      cout << "Destructing now!!! " << (int)this /*unique identifier*/ << endl;
      }

      // public function to return private value of variable x
      float Point::getX()
      {
      return x;
      }

      int main()
      {
      // create instance of class
      Point myLocation(23, 54, 32);

      // create another instance of class
      //Point anotherLocation;
      
      // calling constructor with args
      //Point thirdLocation(23, 14, 52);
      
      // return value of x
      cout << myLocation.getX() << endl;
      
      return 0;
      

      }

      M Offline
      M Offline
      Md Ali Naser Khan
      wrote on last edited by
      #2

      You are trying to get float variable X which has no assigned value. Point::Point(float f_x, float f_y, float f_z) { // Assign value to your variable x = f_x; y = f_y; z = f_z; cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl; }

      M 1 Reply Last reply
      0
      • M Md Ali Naser Khan

        You are trying to get float variable X which has no assigned value. Point::Point(float f_x, float f_y, float f_z) { // Assign value to your variable x = f_x; y = f_y; z = f_z; cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl; }

        M Offline
        M Offline
        Michael Randolph
        wrote on last edited by
        #3

        When I instantiate the object with this:

        // create instance of class
        Point myLocation(23, 54, 32);

        That should assign values 23, 54, 32 to x, y, and z, respectively.

        M 1 Reply Last reply
        0
        • M Michael Randolph

          When I instantiate the object with this:

          // create instance of class
          Point myLocation(23, 54, 32);

          That should assign values 23, 54, 32 to x, y, and z, respectively.

          M Offline
          M Offline
          Md Ali Naser Khan
          wrote on last edited by
          #4

          No. you are wrong here. If you debug your code you can see the wrong.

          M 1 Reply Last reply
          0
          • M Md Ali Naser Khan

            No. you are wrong here. If you debug your code you can see the wrong.

            M Offline
            M Offline
            Michael Randolph
            wrote on last edited by
            #5

            I thought once you instantiate the object it sets the variables that I have for parameters in the function? Where am I forgetting something? This should work as far as I can see.

            C N 2 Replies Last reply
            0
            • M Michael Randolph

              I thought once you instantiate the object it sets the variables that I have for parameters in the function? Where am I forgetting something? This should work as far as I can see.

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #6

              Michael Randolph wrote:

              I thought once you instantiate the object it sets the variables that I have for parameters in the function? Where am I forgetting something? This should work as far as I can see.

              I think you are seriously mistaken here: the compiler is no magic. You declare a constructor and pass two variables in that constructor. If you don't do anything with those values, the compiler won't magically assign them to your class members. That would be crazy... You have to assign them yourself as the other poster already told you.

              Michael Randolph wrote:

              I thought once you instantiate the object it sets the variables that I have for parameters in the function?

              Once again, the compiler is no magic, it can't guess what you want to do with the variables. What if you pass less variables than you have class members (or more) ? What if they are in different order ? In most of the cases, a class is much more than two member variables (sometimes around 100 members) and in general you pass only a few of them in the constructor. So, how will the compiler know tho which member variables he has to assign those ? That would be crazy...

              Cédric Moonen Software developer
              Charting control [v1.5] OpenGL game tutorial in C++

              1 Reply Last reply
              0
              • M Michael Randolph

                I am compiling with g++ 4.2.4 on Ubuntu Hardy. I am trying to creating a public function to return a value of a private variable within the class. The output I am getting is not correct. What did I miss here?

                #include <iostream>

                using namespace std;

                class Point
                {
                public:
                // constructors
                Point(float f_x = 1.0, float f_y = 1.0, float f_z = 1.0);

                // destructors (Only necessary for new dynamic memory created)
                ~Point();
                
                float getX();
                

                private:
                float x, y, z;

                protected:

                };

                // define constructor with args
                Point::Point(float f_x, float f_y, float f_z)
                {
                cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl;
                }

                // define destructor
                Point::~Point()
                {
                cout << "Destructing now!!! " << (int)this /*unique identifier*/ << endl;
                }

                // public function to return private value of variable x
                float Point::getX()
                {
                return x;
                }

                int main()
                {
                // create instance of class
                Point myLocation(23, 54, 32);

                // create another instance of class
                //Point anotherLocation;
                
                // calling constructor with args
                //Point thirdLocation(23, 14, 52);
                
                // return value of x
                cout << myLocation.getX() << endl;
                
                return 0;
                

                }

                S Offline
                S Offline
                Stuart Dootson
                wrote on last edited by
                #7

                Change the constructor to assign the constructor parameters to the members, either using initialiser syntax:

                Point::Point(float f_x, float f_y, float f_z) : x(f_x), y(f_y), z(f_z)
                {
                cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl;
                }

                or explicit assignments

                Point::Point(float f_x, float f_y, float f_z)
                {
                x = f_x;
                y = f_y;
                z = f_z;
                cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl;
                }

                As Cedric said - the compiler ain't magic - it doesn't look for similarities between parameter names and data member names and decide to assign parameters to data members automatically!

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                M 1 Reply Last reply
                0
                • M Michael Randolph

                  I thought once you instantiate the object it sets the variables that I have for parameters in the function? Where am I forgetting something? This should work as far as I can see.

                  N Offline
                  N Offline
                  Niklas L
                  wrote on last edited by
                  #8

                  Calling a constructor is no different from calling a normal function in this sense. The arguments supplied needs to be handled explicitly.

                  home

                  1 Reply Last reply
                  0
                  • S Stuart Dootson

                    Change the constructor to assign the constructor parameters to the members, either using initialiser syntax:

                    Point::Point(float f_x, float f_y, float f_z) : x(f_x), y(f_y), z(f_z)
                    {
                    cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl;
                    }

                    or explicit assignments

                    Point::Point(float f_x, float f_y, float f_z)
                    {
                    x = f_x;
                    y = f_y;
                    z = f_z;
                    cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl;
                    }

                    As Cedric said - the compiler ain't magic - it doesn't look for similarities between parameter names and data member names and decide to assign parameters to data members automatically!

                    Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                    M Offline
                    M Offline
                    Michael Randolph
                    wrote on last edited by
                    #9

                    ahh I see. That makes so much more sense! Thanks for your responses. My next question deals with inheritance. It appears that the = operator declaration is not being inherited from my class Point. I am getting this compiling error:

                    g++ -o inheritance inheritance.cpp
                    inheritance.cpp: In function ‘int main()’:
                    inheritance.cpp:165: error: no match for ‘operator=’ in ‘vect3 = Vector::operator+(Vector&)(((Vector&)(& vect2)))’
                    inheritance.cpp:135: note: candidates are: Vector& Vector::operator=(Vector&)

                    here is my source:

                    #include <iostream>

                    using namespace std;

                    class Point
                    {
                    public:
                    Point(float f_x = 1.0, float f_y = 1.0, float f_z = 1.0);
                    ~Point();

                    // return by reference
                    void getXYZ(float &X, float &Y, float &Z);
                    float getX();
                    float getY();
                    float getZ();
                    
                    void setXYZ(float X, float Y, float Z);
                    void setX(float X);
                    void setY(float Y);
                    void setZ(float Z);	
                    
                    // overloading = operator to deal with pointers
                    Point operator = (Point &p);
                    

                    private:
                    float x, y, z;

                    protected:

                    };

                    // define constructor with args
                    Point::Point(float f_x, float f_y, float f_z)
                    {
                    cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl;
                    x = f_x;
                    y = f_y;
                    z = f_z;
                    }

                    // define destructor
                    Point::~Point()
                    {
                    cout << "Destructing now!!! " << (int)this /*unique identifier*/ << endl;
                    }

                    // public function to return private value of variable x
                    float Point::getX()
                    {
                    return x;
                    }

                    // public function to return private value of variable y
                    float Point::getY()
                    {
                    return y;
                    }

                    // public function to return private value of variable z
                    float Point::getZ()
                    {
                    return z;
                    }

                    // public function to change value of private variable x
                    void Point::setX(float X)
                    {
                    x = X;
                    }

                    // public function to change value of private variable y
                    void Point::setY(float Y)
                    {
                    y = Y;
                    }

                    // public function to change value of private variable z
                    void Point::setZ(float Z)
                    {
                    z = Z;
                    }

                    // public function to change value of all three private variables
                    void Point::setXYZ(float X, float Y, float Z)
                    {
                    /* slower
                    setX(X);
                    setY(Y);
                    setZ(Z);*/
                    // faster
                    x = X;
                    y = Y;
                    z = Z;

                    }

                    // public function to return values of x,y,z by reference
                    void Point::getXYZ(float &X, float &Y, float &Z)
                    {
                    X = getX();
                    Y = getY();
                    Z = getZ();
                    }

                    // overloading operator << (output stream)
                    ostream &operator <<(ostream &stream, Point &p)
                    {
                    stream << p.getX() <

                    S 1 Reply Last reply
                    0
                    • M Michael Randolph

                      ahh I see. That makes so much more sense! Thanks for your responses. My next question deals with inheritance. It appears that the = operator declaration is not being inherited from my class Point. I am getting this compiling error:

                      g++ -o inheritance inheritance.cpp
                      inheritance.cpp: In function ‘int main()’:
                      inheritance.cpp:165: error: no match for ‘operator=’ in ‘vect3 = Vector::operator+(Vector&)(((Vector&)(& vect2)))’
                      inheritance.cpp:135: note: candidates are: Vector& Vector::operator=(Vector&)

                      here is my source:

                      #include <iostream>

                      using namespace std;

                      class Point
                      {
                      public:
                      Point(float f_x = 1.0, float f_y = 1.0, float f_z = 1.0);
                      ~Point();

                      // return by reference
                      void getXYZ(float &X, float &Y, float &Z);
                      float getX();
                      float getY();
                      float getZ();
                      
                      void setXYZ(float X, float Y, float Z);
                      void setX(float X);
                      void setY(float Y);
                      void setZ(float Z);	
                      
                      // overloading = operator to deal with pointers
                      Point operator = (Point &p);
                      

                      private:
                      float x, y, z;

                      protected:

                      };

                      // define constructor with args
                      Point::Point(float f_x, float f_y, float f_z)
                      {
                      cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl;
                      x = f_x;
                      y = f_y;
                      z = f_z;
                      }

                      // define destructor
                      Point::~Point()
                      {
                      cout << "Destructing now!!! " << (int)this /*unique identifier*/ << endl;
                      }

                      // public function to return private value of variable x
                      float Point::getX()
                      {
                      return x;
                      }

                      // public function to return private value of variable y
                      float Point::getY()
                      {
                      return y;
                      }

                      // public function to return private value of variable z
                      float Point::getZ()
                      {
                      return z;
                      }

                      // public function to change value of private variable x
                      void Point::setX(float X)
                      {
                      x = X;
                      }

                      // public function to change value of private variable y
                      void Point::setY(float Y)
                      {
                      y = Y;
                      }

                      // public function to change value of private variable z
                      void Point::setZ(float Z)
                      {
                      z = Z;
                      }

                      // public function to change value of all three private variables
                      void Point::setXYZ(float X, float Y, float Z)
                      {
                      /* slower
                      setX(X);
                      setY(Y);
                      setZ(Z);*/
                      // faster
                      x = X;
                      y = Y;
                      z = Z;

                      }

                      // public function to return values of x,y,z by reference
                      void Point::getXYZ(float &X, float &Y, float &Z)
                      {
                      X = getX();
                      Y = getY();
                      Z = getZ();
                      }

                      // overloading operator << (output stream)
                      ostream &operator <<(ostream &stream, Point &p)
                      {
                      stream << p.getX() <

                      S Offline
                      S Offline
                      Stuart Dootson
                      wrote on last edited by
                      #10

                      OK - a couple of issues there. Firstly, your operator= isn't inherited because the signature of operator= in Point doesn't conform to what the generated operator= in Vector wants to see - it wants this:

                      Point& operator = (Point const &p);

                      i.e. take a const reference and return a non-const reference. Having said that, returning Point by value rather than reference is also a conformant signature, as C++ function signatures depend on parameter types and CV qualification (for methods), not return value - but why return Point by value anyway? Secondly, make your getters const methods - then they can be used on const references, as (now) taken by your operator=:

                      void getXYZ(float &X, float &Y, float &Z) const;
                      float getX() const;
                      float getY() const;
                      float getZ() const;
                      

                      If you make those two changes, then your Vector's generated operator= will compile successfully.

                      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                      M 1 Reply Last reply
                      0
                      • S Stuart Dootson

                        OK - a couple of issues there. Firstly, your operator= isn't inherited because the signature of operator= in Point doesn't conform to what the generated operator= in Vector wants to see - it wants this:

                        Point& operator = (Point const &p);

                        i.e. take a const reference and return a non-const reference. Having said that, returning Point by value rather than reference is also a conformant signature, as C++ function signatures depend on parameter types and CV qualification (for methods), not return value - but why return Point by value anyway? Secondly, make your getters const methods - then they can be used on const references, as (now) taken by your operator=:

                        void getXYZ(float &X, float &Y, float &Z) const;
                        float getX() const;
                        float getY() const;
                        float getZ() const;
                        

                        If you make those two changes, then your Vector's generated operator= will compile successfully.

                        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                        M Offline
                        M Offline
                        Michael Randolph
                        wrote on last edited by
                        #11

                        Thank you so much for your help. Looks like I have a bit of studying to do. I am not totally sure how this works but it does. Here is the fixed source in case someone needs to check it out.

                        #include <iostream>

                        using namespace std;

                        class Point
                        {
                        public:
                        Point(float f_x = 1.0, float f_y = 1.0, float f_z = 1.0);
                        ~Point();

                        void setXYZ(float X, float Y, float Z);
                        void setX(float X);
                        void setY(float Y);
                        void setZ(float Z);
                        
                        // return by reference
                        void getXYZ(float &X, float &Y, float &Z);
                        float getX() const;
                        float getY() const;
                        float getZ() const;
                        
                        // overloading = operator to deal with pointers
                        Point& operator =(Point const &p);
                        

                        private:
                        float x, y, z;

                        protected:

                        };

                        // define constructor with args
                        Point::Point(float f_x, float f_y, float f_z)
                        {
                        cout << "We're in the constructor with arguments " << (int)this /*unique identifier*/ << endl;
                        x = f_x;
                        y = f_y;
                        z = f_z;
                        }

                        // define destructor
                        Point::~Point()
                        {
                        cout << "Destructing now!!! " << (int)this /*unique identifier*/ << endl;
                        }

                        // public function to return private value of variable x
                        float Point::getX() const
                        {
                        return x;
                        }

                        // public function to return private value of variable y
                        float Point::getY() const
                        {
                        return y;
                        }

                        // public function to return private value of variable z
                        float Point::getZ() const
                        {
                        return z;
                        }

                        // public function to change value of private variable x
                        void Point::setX(float X)
                        {
                        x = X;
                        }

                        // public function to change value of private variable y
                        void Point::setY(float Y)
                        {
                        y = Y;
                        }

                        // public function to change value of private variable z
                        void Point::setZ(float Z)
                        {
                        z = Z;
                        }

                        // public function to change value of all three private variables
                        void Point::setXYZ(float X, float Y, float Z)
                        {
                        /* slower
                        setX(X);
                        setY(Y);
                        setZ(Z);*/
                        // faster
                        x = X;
                        y = Y;
                        z = Z;

                        }

                        // public function to return values of x,y,z by reference
                        void Point::getXYZ(float &X, float &Y, float &Z)
                        {
                        X = getX();
                        Y = getY();
                        Z = getZ();
                        }

                        // overloading = operator
                        Point& Point::operator =(Point const &p)
                        {
                        setX(p.getX());
                        setY(p.getY());
                        setZ(p.getZ());
                        // passing back value rather than address
                        return *this;
                        }

                        // overloading operator << (output stream)
                        ostream &operator <<(ostream &stream, Point &p)
                        {
                        stream << p.getX() << " " << p.getY() << " " << p.getZ();
                        return stream;
                        }

                        // overloading operator >> (input

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups