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 8051819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:23:17+00:00 2026-06-05T07:23:17+00:00

I have a set of utility classes that store static const data members. Now

  • 0

I have a set of utility classes that store static const data members. Now I need to use these data members in functional classes. I am planning to use references (dont want pointers) to the static const objects, but keep getting the error below. Can you please point out the logical/technical mistake in the code?

#include <string>

class staticData
{
  public:
   static const int cs = 1;
   static const staticData data1;

  private:
   staticData(int id_): _id(id_)   //NOTE: Private constructer, static access only!!
   { }

   int _id;
};

const staticData staticData::data1(1001);

class testReference
{
  public:
    testReference(): _member(staticData::data1)
    {}

  private:
    staticData& _member;
};

invalid initialization of reference of type âstaticData&â from expression of type âconst staticDataâ

  • 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-05T07:23:20+00:00Added an answer on June 5, 2026 at 7:23 am

    You’re attempting to reference a const object through a non-const reference.

    Thus, the original object can be modified through the reference, as the reference is non-const, and thus you’re breaking the contract you made when declaring the object as const.

    There are 2 options:

    • remove the const from static const staticData data1;
    • make the reference const: const staticData& _member;

    EDIT:

    As per your comment, you can have:

    class testReference
    {
      public:
        testReference(): _member(&staticData::data1)
        {}
    
      private:
        staticData const * _member;
    };
    

    This way, you can change what _member points to (not possible with references), but you can’t change the object itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of methods that do some utility work over SQL connection,
We have these set of utility constants defined in a series of file. The
I have a set of files for a custom-made CMS, and now that the
We have a static method in a utility class that will download a file
I have set-up a SkyDrive account to use as an off-site backup so when
I am doing Android programing with Eclipse (Helios), and I have some utility classes
I've been dealing a lot lately with abstract classes that use generics. This is
We have a utility that will run any day between Monday - Friday. It
I have the following code. The intent is to have this utility class that
I have a set of date/time strings in the YYYYMMDDHHMMSS format that I want

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.