A simple question of a C++ function

This is a discussion on A simple question of a C++ function within the C++ Programming forums, part of the General Programming Boards category; Here is a class Code: class Complex{ private: int real, image; public: Complex(int r=0, int i=0):real(r),image(i){} int GetReal() {return real;} ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    A simple question of a C++ function

    Here is a class

    Code:
    class Complex{
    private:
    int real, image;
    public:
    Complex(int r=0, int i=0):real(r),image(i){}
    
    int GetReal() {return real;}
    int GetImage() {return image;}
    
    void foo(const Complex & rhs){
    cout<<rhs.GetImage()<<endl;
    return;
    }
    };
    When I compile it, it reports:

    In foo method: error C2662: 'Complex::GetImage' : cannot convert 'this' pointer from 'const Complex' to 'Complex &'
    What does it mean?

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Since rhs is a const object, you can only call its members which are marked const, so try this:
    Code:
    int GetReal() const {return real;}
    int GetImage() const {return image;}
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Thank you. I revised the function as:

    Code:
    void foo(const Complex & rhs){
    cout<<rhs.image<<endl;
    return;
    }
    It works, but my question is that 'image' is private of 'rhs', how can you visit a private variable of rhs outside (rhs.image)?

    Quote Originally Posted by JaWiB View Post
    Since rhs is a const object, you can only call its members which are marked const, so try this:
    Code:
    int GetReal() const {return real;}
    int GetImage() const {return image;}

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    JaWiB proposed a good solution to your problem, but you implemented another.

    The reason your change compiles is because classes are friends with themselves, and thus can access private members. I don't think I've ever seen a tutorial mention this, but adding "friend class Complex" to your class will yield this error in gcc:
    Code:
    someclass.cpp:6: error: class `Complex' is implicitly friends with itself
    Last edited by Cactus_Hugger; 05-07-2007 at 07:51 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function name basic question help
    By kenryuakuma in forum C++ Programming
    Replies: 7
    Last Post: 09-24-2008, 08:48 AM
  2. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 01:22 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. multidimensional array function - simple question
    By boltz in forum C++ Programming
    Replies: 6
    Last Post: 05-23-2005, 05:24 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21