Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8749437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:41:42+00:00 2026-06-13T12:41:42+00:00

Here is my code : class X { public: X():_x(5){} void GetVal(int & oVal)

  • 0

Here is my code :

class X
{
public:
  X():_x(5){}

  void GetVal(int & oVal)
  {
    oVal = _x;
  }

 private:
  int _x;

};

class Y
{
public:
  X * const GetX()
  {
    return &_x;
  }
private:
  X _x;
};

int main()
{
  Y y;
  X * p = y.GetX();
  int * pInt = new int[2];
 p->GetVal(pInt[0]);
}

In the last line of main, I get an error

Incorrect access of a member from const-qualified function

This error is observed only when the code is compiled on a sun solaris system and doesn’t happen on windows or aix system. Any idea why?

Also the strangest thing is that the error is gone if I replace pInt[0], with a simple integer (int a = 0; p->GetVal(a))

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:41:43+00:00Added an answer on June 13, 2026 at 12:41 pm

    The const in X * const GetX() will be ignored, because the result of the function call is an rvalue and rvalues of non class type cannot be const according to c++ const member function that returns a const pointer.. But what type of const is the returned pointer?.

    Are you sure you didn’t mean to write:

    const X * GetX() const
    {
      return &_x;
    }
    

    that is, you change it from returning a constant pointer to variable date into a variable pointer to constant data, and you declare GetX() to be a constant member function, that is a member function that can be used on constant instances of Y: const Y y;

    Furthermore in class X, you can change GetVal() to

    void GetVal(int & oVal) const
    {
      oVal = _x;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code: class Time { public static void printTime (int hour, int
Here is my somewhat odd code: template <typename T&> class A { public: void
Here is my code: class Soldier { public: Soldier(const string &name, const Gun &gun);
Here's my Code Class A public class A { private int a; public int
Here is the code: class Test { public static void main (String[] args) throws
Here is the code, it's pretty straightforward. class Foo { public: void print() {
Here is my code: public partial class Image : System.Web.UI.Page { protected void Page_Load(object
Here's the code: class qual { public static int fibonacci(int n) { if (n
Here is my code: public class Test { static { main(null); } public static
here is the code (java): class prime { public static boolean prime (int a,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.