Thread: a data member that has no default contructor?

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    28

    a data member that has no default contructor?

    hi Guys,

    i basicly want a class that has no default constructor as a data member of another class. something like this:

    Code:
    class A
    {
    public:
    	A( int x );
    };
    
    class B //other class
    {
    private:
    	A objectA;
    public:
    	B( int x );
    };
    A B::objectA = A( 0 );
    my question: can it be done?
    i would appreciate any help

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    Code:
    class A {
      public:
        A (int x) {};
    };
    
    class B {
      public:
        B (int x) : m_A (x) {}
    
      private:
        A m_A;
    };
    
    int
    main (int argc, char *argv[])
    {
      B foo (2);
    
      return 0;
    }

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Are you asking how to prevent people from using the default constructor?
    Simple, make the default constructor private.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cpjust
    Are you asking how to prevent people from using the default constructor?
    I think symbiote is asking how to initialise a member variable of a type that has no default constructor, in which case edoceo's suggestion of using the constructor initialisation list is correct.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by laserlight View Post
    I think symbiote is asking how to initialise a member variable of a type that has no default constructor, in which case edoceo's suggestion of using the constructor initialisation list is correct.
    Ah yes, after re-reading it, now that makes sense.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    28
    thanks for the replies.
    i almost forgot i asked this question.
    i wanted a class that must to be initialized.
    thanks edoceo. that's exactly what i was looking for.

    Greetings

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  2. Static & Data Member
    By ecoliteracy in forum C++ Programming
    Replies: 1
    Last Post: 04-16-2007, 08:46 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. How do I base size of arrays on annother number?
    By Dual-Catfish in forum C++ Programming
    Replies: 15
    Last Post: 09-25-2001, 01:31 PM
  5. Initilize data member
    By xstone in forum C++ Programming
    Replies: 6
    Last Post: 09-24-2001, 05:38 PM