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

  • Home
  • SEARCH
  • 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 6534847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:18:01+00:00 2026-05-25T10:18:01+00:00

Suppose you have a class with a member variable declared constant. class Test {

  • 0

Suppose you have a class with a member variable declared constant.

class Test
{
public:
    Test(const int const_param) : const_member_(const_param) {}

private:
    const int const_member_;
};

We want a function that will return an instance of this object either as a “return value”

Test Foo();  
Test* Foo();     
Test& Foo();

or as an “output parameter” (i.e. a pointer that is passed in).

void Foo(Test* test);
void Foo(Test** test); // maybe this is more correct?

Note this function is the only thing that can create the object (in the above example, Foo will be the only thing that knows the value of const_param and can thus create a Test object)

What would be the best way to do something like this? Is this even possible?

  • 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-25T10:18:02+00:00Added an answer on May 25, 2026 at 10:18 am

    You can just return such an object by copy, unless you have good reason do avoid this design:

    Foo make_foo()
    {
      int n = get_mystery_value()
      Foo x(n);
      x.manipulate();
      return x;
    }
    

    If you’d rather handle the object by pointer, use std::shared_ptr<Foo> instead:

    std::shared_ptr<Foo> make_foo()
    {
      int n = roll_dice();
      auto px = std::make_shared<Foo>(n);
      px->crazy_stuff();
      return px;
    };
    

    Or, if you just need one handler object, use std::unique_ptr<Foo>.


    A bit of explanation: Having a constant member in your object essentially means that the object itself has the semantics of a constant object. It is OK to copy constants, but not to reassign them, so in your typical use case you would only create an object of this class once and not reassign it. A copy constructor is automatically defined for you, so you can go straight ahead and say:

    int main()
    {
      Foo x = make_foo();
      Foo y(make_foo());   // same thing
    
      x.twiddle();
      y.skedaddle();
    
      // ...
    }
    

    Clearly you cannot and would not say x = make_foo();, because your x is semantically a constant thing for which reassignment doesn’t make sense.

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

Sidebar

Related Questions

Suppose I have a class module clsMyClass with an object as a member variable.
Suppose I have a class public class MyClass { private Set<String> set = new
Suppose I have a class Base which has a member variable A* my_hash. I
Suppose I have: public class Bob { public int Value { get; set; }
For example, suppose I have a class: class Foo { public: std::string& Name() {
Suppose I have some per-class data: (AandB.h) class A { public: static Persister* getPersister();
Suppose I have the following class: public class TestBase { public bool runMethod1 {
Suppose I initialize members of a class like this: class A { public int
Suppose I have a class MyClass to which I want to add certain 'observer'
Suppose I have a class with some attributes. How is it best (in the

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.