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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:51:36+00:00 2026-05-26T14:51:36+00:00

I have a class set up like this: class Foo { Foo(); private: Bar

  • 0

I have a class set up like this:

class Foo {
  Foo();    

  private:
     Bar m_bar;
}

That is the class definition in it’s own header file, and now in the source file to go with it I have the constructor and I tried doing this:

Foo::Foo() {
  m_bar("parameters for the Bar constructor");
}

However this doesn’t work and gives me an error. I can make m_bar a pointer and then in Foo’s constructor do this:

m_bar = new Bar("parameters here");

However that makes m_bar a pointer and I don’t want that.

I’m not the best with C++ classes and pointers, so could something either explain a way for me to have m_bar defined in the Foo class but constructor somewhere else or if it is better to make m_bar a pointer in this situation explain why? While I would rather not make it a pointer(because I don’t understand pointers extremely well), if that is the best way to do it then I would rather do it that way, but I’d still like someone to explain why that is the best way to do it(if it is).

  • 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-26T14:51:37+00:00Added an answer on May 26, 2026 at 2:51 pm

    Yes, using the initializer list syntax:

    Foo::Foo() : 
      m_bar("parameters for the Bar constructor")
    {
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class like this: private class MyClass { [DisplayName(Foo/Bar)] public string FooBar
I have a Hibernate mapping that looks something like this: <class name=MyEntity> <set name=scalarSet
Assume I have a class like this: public class Foo { public Bar RequiredProperty
If you have an object like this: public class Foo { private int _id;
I currently have my PHP class variables set up like this: class someThing {
I have something like this set up: class CategoryPage (webapp.RequestHandler): def get(self): ** DO
I have a two models set up like this: class User < ActiveRecord::Base #
I have a class like this, public class person { name Name {set; get;}
like in java I have: Class.getSuperClass().getDeclaredFields() how I can know and set private field
I have a class Foo which looks like this class Foo { public: void

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.