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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:46:21+00:00 2026-05-26T21:46:21+00:00

let’s assume that I have some codes like this. What should I write there

  • 0

let’s assume that I have some codes like this. What should I write there in order to have a constructor that initializes a Foo typed object. Thanks. this is the example I am trying to understand http://flylib.com/books/en/2.674.1.140/1/ sorry for the delay.

class Foo
{
private:
   set<int*> numbers;
public:
   //constuctor
   Foo(set<int*>& numb = "what to write here?"): numbers("something") {};

};
  • 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-26T21:46:22+00:00Added an answer on May 26, 2026 at 9:46 pm

    Having a set of pointers usually isn’t what you want. sets are a collection of unique items, but a set of pointers means each pointer will be unique, not the values they point to.

    Steve Jessop points out some more reasons not to want a container of pointers. For one, it’s not clear who owns the pointers at what point, and so it’s not clear how to handle errors. Part of sorting that out could mean imposing stricter requirements on the pointers, for example requiring them to be new allocated so that the object taking ownership knows how to release them. Such requirements will probably only be enforced by manual inspection of the code and documentation, and so will be error prone. That could be improved on by using smart pointers instead of raw pointers.

    But if for some reason you really do want this with raw pointers, then:

       Foo(set<int*> const &numb = set<int*>()): numbers(numb) {}
    

    Or use overloading instead:

       Foo() {} // default constructs numbers
       Foo(set<int*> const &numb) : numbers(numb) {}
    

    Or if you really want to construct numbers as a non-empty set by default you can do the following (dangerous, leak prone) things:

       Foo(set<int*> const &numb = set<int*>()) : numbers(numb) {
           if(numbers.empty()) { // this also prevents the user from being able to make the set empty
               numbers.insert(new int(0));
               numbers.insert(new int(0));
           }
       }
    

    Or using C++11:

       Foo(set<int*> const &numb = set<int*>{new int(0),new int(0)} ) : numbers(numb) {} // uses default argument value with brace initializer list style
    

    or

       Foo(set<int*> const &numb) : numbers(numb) {}
       Foo() : Foo(set<int*>{new int(0),new int(0)}) {} // uses delegating constructors
    

    Fixing the leaks above using smart pointers yields something like:

       Foo(set<unique_ptr<int>> const &numb = set<unique_ptr<int>>()) : numbers(numb) {
           if(numbers.empty()) { // this also prevents the user from being able to make the set empty
               numbers.insert(unique_ptr<int>(new int(0)));
               numbers.insert(unique_ptr<int>(new int(0)));
           }
       }
    

    Using brace initialization with smart pointers like the following still has the possibility for leaks:

       Foo(set<unique_ptr<int>> const &numb = set<int*>{unique_ptr<int>(new int(0)),unique_ptr<int>(new int(0))} ) : numbers(numb) {}
    

    So instead you should use a helper function to produce the default value:

    class Foo
    {
    private:
        set<unique_ptr<int>> numbers;
        static set<unique_ptr<int>> default_set() {
            set<unique_ptr<int>> the_default;
            the_default.insert(unique_ptr<int>(new int(0)));
            the_default.insert(unique_ptr<int>(new int(0)));
            return the_default;
        }
    public:
        // using default arg
        Foo(set<unique_ptr<int>> &&numb = default_set()) : numbers(move(numb)) {};
    
        // or delegating constructors
        Foo(set<unique_ptr<int>> &&numb) : numbers(move(numb)) {};
        Foo() : Foo(default_set()) {}
    
        // in addition to one of the above you probably want a constructor that takes a non-rvalue-reference and copies it
        Foo(set<unique_ptr<int>> numb) : Foo(move(numb)) {}; // copy is implicit, we delegate moving that copy into number to the ctor that takes an rvalue-reference. You could also just initialize numbers here directly.
    
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that I have a set of relations that looks like this: relations
Let's say that I have classes like this: public class Parent { public int
Let's say I have saved this kind of data (some text '.date(d).' some text)
Let's say I have some content classes like Page, TabGroup, Tab, etc. Certain of
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say there is a graph and some set of functions like: create-node ::
Let's say I have table with column 'URL' whrere I store urls like this
Let's say i have this block of code, <div id=id1> This is some text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Let's assume that a user votes for some movies in a scale of 1

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.