Constructor help

This is a discussion on Constructor help within the C++ Programming forums, part of the General Programming Boards category; Given:- Code: class A { public: A() { cout << "A constructed\n"} A(int i) : x(i) {} private: int x; ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    55

    Constructor help

    Given:-
    Code:
    class A
    {
        public:
            A() { cout << "A constructed\n"}
            A(int i) : x(i) {}
    
        private:
            int x;
    };
    
    int main()
    {
        vector < A > temp;
    
        A();
    
        temp.push_back(A(3));
    
        system("pause");
        return 0;
    }
    What do you mean by invoking the constructor just like that ?
    What do you mean by passing the constrctor into the vector temp ?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What do you mean by saying "What do you mean" for either of those sentences? Oh, I see. This is HOMEWORK. Yeah. We don't do that here.

    [edit=wtf]homeword? I need more coffee... [/edit]

    Quzah.
    Last edited by quzah; 11-03-2004 at 01:41 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by quzah
    What do you mean by saying "What do you mean" for either of those sentences? Oh, I see. This is HOMEWORD. Yeah. We don't do that here.

    Quzah.
    I thought it was homework we didn't do for them

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't do either.

    :sip: Must wake up. :sip:

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    55
    well, it is not homework. I just come across a source code on FP Tree and i saw in it that they just invoked the constructor and pass constructor in a method or ADT just like something i mentioned above. I got confused and don't understand why. Thats why i come here to look for some answers. Hope someone here can clarify my doubts.

    If u want, i can post the source code

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What do you mean by invoking the constructor just like that ?
    To print a message, apparently. Not exactly the most robust use of a constructor for a class that has members needing initialization that I've seen though.

    >What do you mean by passing the constrctor into the vector temp ?
    What does a constructor do? What does push_back for std::vector do? Using those two intuitive questions, you can make a reasonable guess.

    >Hope someone here can clarify my doubts.
    I absolutely despise the use of the word doubt for this meaning. It's almost as bad as "I have a doubt about <so and so feature>" when it should be "I have a question about <so and so feature>". This differences in meaning are very subtle, but doubt is an awful choice of words almost every time I see it in a programming question. Even when it's actually meaningful, the definition isn't a commonly used one, so it still feels incorrect even though it's not, and that just bugs me.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    In the material I've read I've not seen the syntax of user declaring nameless objects; for example A() and A(3) in the OPs code. Is this common/accepted practice?

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    55
    so it is possible to invoked the constructor with creating any object out of its class ? When pass a constructor into the vector, so it is like putting an object into it ? If so, what is the difference between putting a constructor into a vector with putting an object into a vector ?

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    A constructor is called whenever a class is instantied
    Calling A(); create an instance of class A, which is then despised, because it's not being stored. If it was A a(); that instance of A would be stored in a.
    This temp.push_back(A(3)); isn't passing a constructor as argument. Try to understand this one.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21