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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:09:10+00:00 2026-05-14T19:09:10+00:00

I’ve got class which is using plain-only-data struct with const variables and I’m not

  • 0

I’ve got class which is using plain-only-data struct with const variables and I’m not sure, if I’m allocating these structures in a proper way. It looks more or less like:

#include <cstdlib>
#include <iostream>
using std::cout;
using std::endl;

struct some_const_struct {
     const int arg1;
     const int arg2;
};

class which_is_using_above_struct {
 private:

  some_const_struct* m_member;

  const some_const_struct* const m_const_member;

 public:

  const some_const_struct& get_member() const { return *m_member; }

  const some_const_struct& get_const_member() const { return *m_const_member; }

  void set_member(const int a, const int b) {
   if(m_member != NULL) {
    delete m_member;
    m_member = NULL;
   }
   m_member = new some_const_struct((some_const_struct){a, b});
  }

  explicit which_is_using_above_struct(const int a, const int b)
   : m_const_member(new some_const_struct((const some_const_struct){a, b})) {
   m_member = NULL;
  }

  ~which_is_using_above_struct() {
   if(m_member != NULL) {
    delete m_member;
   }
   if(m_const_member != NULL) {
    delete m_const_member;
   }
  }

};

int main() {
 which_is_using_above_struct c(1, 2);
 c.set_member(3, 4);
 cout << "m_member.arg1 = " << c.get_member().arg1 << endl;
 cout << "m_member.arg2 = " << c.get_member().arg2 << endl;
 cout << "m_const_member.arg1 = " << c.get_const_member().arg1 << endl;
 cout << "m_const_member.arg2 = " << c.get_const_member().arg2 << endl;
 return 0;
}

I’m just not quite sure if the statement:

m_member = new some_const_struct((some_const_struct){a, b});

doesn’t produce unnessesary use of some_const_struct’s copy constructor, ergo allocating that struct twice. What do you think? And is it reasonable to make that struct’s members const? (they’re not supposed to change in their lifetime at all)

  • 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-05-14T19:09:10+00:00Added an answer on May 14, 2026 at 7:09 pm
    m_const_member(new some_const_struct((const some_const_struct){a, b}))
    

    Such syntax is not allowed by standard C++ and is only supported by your compiler as an extension.

    I think the only standard-compiliant way with the current standard1 would be to use copy-construction and a helper function to initialize the struct in a portable way.2

    inline some_const_struct make_some_const_struct(int a, int b)
    {
        some_const_struct x = {a, b};
        return x;
    }
    
    //usage:
    m_const_member(new some_const_struct(make_some_const_struct(a, b))) 
    

    I think you should just make your life easier and not use const members, particularly since set_member allows you to modify the fields anyway (at a great cost both coding- and performance-wise).

    You could also give constructors3 to the classes, but then they won’t be Plain Old Data structs any more, if that is important to you. (Actually I’m not sure whether a struct with const members qualifies as POD in the first place: e.g memsetting and memcpying such a struct would violate the constness of the members, but should be legal things to do with real POD structs.)


    1 AFAIK, C++0x will revise the rules concerning {} in initialization, making it easier to achieve what you want.

    2 Under favorable conditions, the compiler should be able to optimize out the needless copy, transforming it into the exact equivalent of what you are doing.

    3 Comeau’s compiler appears to be very helpful. It even tells you what (it thinks) you should do: warning: class "some_const_struct" defines no constructor to initialize the following: ...

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

Sidebar

Ask A Question

Stats

  • Questions 379k
  • Answers 379k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer That depends on how you define "shape". Visually, you change… May 14, 2026 at 9:29 pm
  • Editorial Team
    Editorial Team added an answer This issue has been fixed. Here: Assert.Equal("foo", "foo\0") does not… May 14, 2026 at 9:29 pm
  • Editorial Team
    Editorial Team added an answer Swing is double buffered by default so you don't need… May 14, 2026 at 9:29 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.