Call Constructor for object in struct

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David

    Call Constructor for object in struct

    Hello,

    How do I call the constructor when a class object (non-pointer) is in a
    structure?

    struct {
    ...
    CMyClass C;
    } MyStruct;

    MyStruct A, B;

    TIA!!


  • Victor Bazarov

    #2
    Re: Call Constructor for object in struct

    David wrote:
    How do I call the constructor when a class object (non-pointer) is in a
    structure?
    >
    struct {
    ...
    CMyClass C;
    } MyStruct;
    >
    MyStruct A, B;
    >
    TIA!!
    What do you mean by "call a constructor"? What constructors does
    'CMyClass' have? Perhaps this is going to help:

    class CMyClass {
    public:
    CMyClass(int);
    CMyClass(char const*);
    };

    ...

    MyStruct A = { ..., CMyClass(42) }, B = { ..., CMyClass("David ") };

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask

    Comment

    • Juha Nieminen

      #3
      Re: Call Constructor for object in struct

      David wrote:
      Hello,
      >
      How do I call the constructor when a class object (non-pointer) is in a
      structure?
      >
      struct {
      ...
      CMyClass C;
      } MyStruct;
      >
      MyStruct A, B;
      You write a constructor in your struct which calls the constructor of
      the member class.

      Comment

      • puzzlecracker

        #4
        Re: Call Constructor for object in struct

        On Sep 29, 5:04 pm, Juha Nieminen <nos...@thanks. invalidwrote:
        David wrote:
        Hello,
        >
        How do I call the constructor when a class object (non-pointer) is in a
        structure?
        >
        struct {
          ...
          CMyClass C;
        } MyStruct;
        >
        MyStruct A, B;
        >
          You write a constructor in your struct which calls the constructor of
        the member class.
        I wonder why C++ is in favor of CMyClass(char const*); copy
        constructor then CMyClass(char const &);

        Any ideas?

        Comment

        • Victor Bazarov

          #5
          Re: Call Constructor for object in struct

          puzzlecracker wrote:
          [..]
          I wonder why C++ is in favor of CMyClass(char const*); copy
          constructor then CMyClass(char const &);
          >
          Any ideas?
          Please rephrase your statement about C++ and favors. A copy constructor
          for the 'CMyClass' class would be either

          CMyClass(CMyCla ss const &);

          or

          CMyClass(CMyCla ss &);

          Anything else is *not* a copy constructor.

          V
          --
          Please remove capital 'A's when replying by e-mail
          I do not respond to top-posted replies, please don't ask

          Comment

          • puzzlecracker

            #6
            Re: Call Constructor for object in struct

            On Sep 29, 5:25 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
            puzzlecracker wrote:
            [..]
            I wonder why  C++ is in favor of  CMyClass(char const*); copy
            constructor  then      CMyClass(char const &);
            >
            Any ideas?
            >
            Please rephrase your statement about C++ and favors.  A copy constructor
            for the 'CMyClass' class would be either
            >
                 CMyClass(CMyCla ss const &);
            >
            or
            >
                 CMyClass(CMyCla ss &);
            >
            Anything else is *not* a copy constructor.
            >
            V
            --
            Please remove capital 'A's when replying by e-mail
            I do not respond to top-posted replies, please don't ask
            No need to rephrase, you answered it. I'm assuming, by providing one
            of (pointer or reference version) copy ctors, the other one will not
            be generated by compiler by default. Is my assumption correct, aka
            reflected in the standard?

            Comment

            • Victor Bazarov

              #7
              Re: Call Constructor for object in struct

              puzzlecracker wrote:
              On Sep 29, 5:25 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
              >puzzlecracke r wrote:
              >>[..]
              >>I wonder why C++ is in favor of CMyClass(char const*); copy
              >>constructor then CMyClass(char const &);
              >>Any ideas?
              >Please rephrase your statement about C++ and favors. A copy constructor
              >for the 'CMyClass' class would be either
              >>
              > CMyClass(CMyCla ss const &);
              >>
              >or
              >>
              > CMyClass(CMyCla ss &);
              >>
              >Anything else is *not* a copy constructor.
              >>
              >V
              >--
              >Please remove capital 'A's when replying by e-mail
              >I do not respond to top-posted replies, please don't ask
              >
              No need to rephrase, you answered it. I'm assuming, by providing one
              of (pointer or reference version) copy ctors, the other one will not
              be generated by compiler by default. Is my assumption correct, aka
              reflected in the standard?
              Erm... There is *no* "pointer version copy ctor". Copy constructors
              *always* take a reference argument. And, no, if you define some other
              constructor, the compiler will still provide a copy constructor in the
              former interface, if it can (depends whether the members are copyable).

              V
              --
              Please remove capital 'A's when replying by e-mail
              I do not respond to top-posted replies, please don't ask

              Comment

              • James Kanze

                #8
                Re: Call Constructor for object in struct

                On Sep 29, 10:47 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
                David wrote:
                How do I call the constructor when a class object
                (non-pointer) is in a structure?
                struct {
                ...
                CMyClass C;
                } MyStruct;
                MyStruct A, B;
                What do you mean by "call a constructor"?
                According to the standard, you can't "call a constructor:-)".
                At any rate, what he probably wants is to initialize the
                elements.
                What constructors does 'CMyClass' have? Perhaps this is going
                to help:
                class CMyClass {
                public:
                CMyClass(int);
                CMyClass(char const*);
                };
                ...
                MyStruct A = { ..., CMyClass(42) }, B = { ..., CMyClass("David ")};
                Note that in this case, you don't even need the CMyClass. If
                the constructors take a single argument, and aren't declared
                explicit, explicit conversion will take place.

                --
                James Kanze (GABI Software) email:james.kan ze@gmail.com
                Conseils en informatique orientée objet/
                Beratung in objektorientier ter Datenverarbeitu ng
                9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                Comment

                • James Kanze

                  #9
                  Re: Call Constructor for object in struct

                  On Sep 29, 11:25 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:

                  [...]
                  Please rephrase your statement about C++ and favors. A copy constructor
                  for the 'CMyClass' class would be either
                  CMyClass(CMyCla ss const &);
                  or
                  CMyClass(CMyCla ss &);
                  Anything else is *not* a copy constructor.
                  MyClass( MyClass volatile& ) ;
                  MyClass( MyClass const volatile& ) ;

                  :-).

                  (Of course, I've never found a use for volatile except on very
                  basic types, and it doesn't work with most modern compilers
                  anyway. But formally...)

                  --
                  James Kanze (GABI Software) email:james.kan ze@gmail.com
                  Conseils en informatique orientée objet/
                  Beratung in objektorientier ter Datenverarbeitu ng
                  9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                  Comment

                  • David

                    #10
                    Re: Call Constructor for object in struct

                    Yeah, basically that... Would this work:

                    struct {
                    int a,b,c,d,e,f,g,h ,i,j;
                    CMyClass C;
                    } MyStruct;

                    MyStruct A={CMyClass(wha tever))};

                    or would I have to always deal with a through j?

                    How about:

                    struct {
                    int a,b,c,d,e,f,g,h ,i,j;
                    CMyClass C;

                    void Init(void) {
                    a=b=c=d=e=f=g=h =i=j=0;
                    C(whatever)
                    }
                    } MyStruct;

                    MyStruct A;

                    A.Init();

                    ??

                    "Victor Bazarov" <v.Abazarov@com Acast.netwrote in message
                    news:gbres6$r8v $3@news.datemas .de...
                    David wrote:
                    >How do I call the constructor when a class object (non-pointer) is in a
                    >structure?
                    >>
                    >struct {
                    > ...
                    > CMyClass C;
                    >} MyStruct;
                    >>
                    >MyStruct A, B;
                    >>
                    >TIA!!
                    >
                    What do you mean by "call a constructor"? What constructors does
                    'CMyClass' have? Perhaps this is going to help:
                    >
                    class CMyClass {
                    public:
                    CMyClass(int);
                    CMyClass(char const*);
                    };
                    >
                    ...
                    >
                    MyStruct A = { ..., CMyClass(42) }, B = { ..., CMyClass("David ") };
                    >
                    V
                    --
                    Please remove capital 'A's when replying by e-mail
                    I do not respond to top-posted replies, please don't ask
                    >

                    Comment

                    Working...