WHAT IS A CONSTRUCT IN C++?

Hi everyone, can somebody explain to me what a construct is.
I'm... guessing you meant constructor. AFAIK there are no "constructs" in C++. There are data structures, constructors, and a member function of the allocator class called "construct", but no "constructs", per-se.

A constructor is a function that is automatically called when an object is created ("constructed"). Constructors have the same name as the type of the object being constructed, and it may take arguments if written to do so.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "arbitrary_header_that_declares_the_type_Objects"

class House {
public:
    House() { //A default constructor.
        wall_count = 4;
        features = NULL;
    }
    House (int walls) { //A non-default constructor.
        wall_count = walls;
        features = NULL;
    }
    ~House() { //A destructor.
        if (features != NULL)
            delete features;
    }
private:
   int wall_count;
   Objects* features;
   //Our house has no roof! ;_;
};

int main() {
    House basic; //The default constructor is called.
    House octagonal(8); //The non-default constructor we defined earlier is called.
    return 0; //Both houses are automatically destroyed.
}


Does this help?

EDIT: Grammar.
-Albatross
Last edited on
Thank you Albatross, this helps a lot. much appreciated,
Topic archived. No new replies allowed.