What is the purpose of creating a member function with the same type as its class?

I’m sure I’ll botch some of the specific terminology, but what is the difference between object and object2 in this implementation? Is the only purpose of the first method (prototyping the class’s function by the same name of the class) just to save typing? At the end of the code, does object==object2?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>//first method
class Class1{
public:
int i;
Class1 (parameter);
};
Class1::Class1 (parameter){
i = 10;
}
Class1 object1(parameter);
//second method
class Class2{
public:
int i;
void function (parameter);
};
Class2::function (parameter){
i = 10;
}
Class2 object2;
object2.function(parameter);
</code>
<code>//first method class Class1{ public: int i; Class1 (parameter); }; Class1::Class1 (parameter){ i = 10; } Class1 object1(parameter); //second method class Class2{ public: int i; void function (parameter); }; Class2::function (parameter){ i = 10; } Class2 object2; object2.function(parameter); </code>
//first method
class Class1{
public:
        int i;
        Class1 (parameter);
};
Class1::Class1 (parameter){
        i = 10;
}
Class1 object1(parameter);

//second method
class Class2{
public:
        int i;
        void function (parameter);
};
Class2::function (parameter){
        i = 10;
}
Class2 object2;
object2.function(parameter);

(C++11)

Class1 and Class2 should be something like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Class1 {
public:
int i;
Class1(int parameter) : i(parameter) {} // This is a constructor
};
class Class2 {
public:
int i;
void function(int parameter) {i = parameter;} // This is a init function
};
</code>
<code>class Class1 { public: int i; Class1(int parameter) : i(parameter) {} // This is a constructor }; class Class2 { public: int i; void function(int parameter) {i = parameter;} // This is a init function }; </code>
class Class1 {
public:
  int i;
  Class1(int parameter) : i(parameter) {}  // This is a constructor
};

class Class2 {
public:
  int i;
  void function(int parameter) {i = parameter;} // This is a init function
};

and

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Class1 object1(10);
Class2 object2;
object2.function(10);
</code>
<code>Class1 object1(10); Class2 object2; object2.function(10); </code>
Class1 object1(10);
Class2 object2;
object2.function(10);

The value of i will be the same: object1.i == object2.i but the purpose of the constructor is NOT to save typing.

  • as Joel said in a comment , with the constructor it’s mandatory to assign a value to i, while with the init function you could use i without a previous explicit assignment.

    In properly designed OO-code the constructor is responsible for establishing the class invariants (here i should be a private data member). Without a constructor each use of the class must first ensure that the object has been properly built.

    You cannot re-initialize an object of a class using its constructor (*) but you can use operator=.

  • const objects can be created only via constructor:

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code>const Class1 object1(10);
    </code>
    <code>const Class1 object1(10); </code>
    const Class1 object1(10);
    

    you cannot write:

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code>const Class2 object; // error (uninitialized const object)
    object2.function(10); // error (attempt to change the const object)
    </code>
    <code>const Class2 object; // error (uninitialized const object) object2.function(10); // error (attempt to change the const object) </code>
    const Class2 object;   // error (uninitialized const object)
    object2.function(10);  // error (attempt to change the const object)
    
  • constructor is always more or equally efficient as having code outside

  • many other reasons (see references).

Technicalities

  • it’s possible to re-initialize an object of a class using its constructor via placement new. Of course it’s ugly beyond description.
  • there are reasons to prefer init functions. Init functions are able to call virtual functions on the object. This doesn’t always work in a constructor, because in the constructor of the base class, the derived class part of the object “doesn’t exist yet”, and in particular you can’t access virtual functions defined in the derived class. Instead, the base class version of the function is called, if defined (see also Why use an initialization method instead of a constructor?).

References

  • C++ – Constructor or Initialize Method to Startup
  • Arguments against “initialize()” method instead of constructors

2

I believe I’ve found the answer at cplusplus.com

It looks like I was correct, but that I should also keep in mind that the ‘constructor’ is only called at creation of the object and can never be called again per that object, whereas a function within the class can be called repeatedly.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

What is the purpose of creating a member function with the same type as its class?

I’m sure I’ll botch some of the specific terminology, but what is the difference between object and object2 in this implementation? Is the only purpose of the first method (prototyping the class’s function by the same name of the class) just to save typing? At the end of the code, does object==object2?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>//first method
class Class1{
public:
int i;
Class1 (parameter);
};
Class1::Class1 (parameter){
i = 10;
}
Class1 object1(parameter);
//second method
class Class2{
public:
int i;
void function (parameter);
};
Class2::function (parameter){
i = 10;
}
Class2 object2;
object2.function(parameter);
</code>
<code>//first method class Class1{ public: int i; Class1 (parameter); }; Class1::Class1 (parameter){ i = 10; } Class1 object1(parameter); //second method class Class2{ public: int i; void function (parameter); }; Class2::function (parameter){ i = 10; } Class2 object2; object2.function(parameter); </code>
//first method
class Class1{
public:
        int i;
        Class1 (parameter);
};
Class1::Class1 (parameter){
        i = 10;
}
Class1 object1(parameter);

//second method
class Class2{
public:
        int i;
        void function (parameter);
};
Class2::function (parameter){
        i = 10;
}
Class2 object2;
object2.function(parameter);

(C++11)

Class1 and Class2 should be something like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Class1 {
public:
int i;
Class1(int parameter) : i(parameter) {} // This is a constructor
};
class Class2 {
public:
int i;
void function(int parameter) {i = parameter;} // This is a init function
};
</code>
<code>class Class1 { public: int i; Class1(int parameter) : i(parameter) {} // This is a constructor }; class Class2 { public: int i; void function(int parameter) {i = parameter;} // This is a init function }; </code>
class Class1 {
public:
  int i;
  Class1(int parameter) : i(parameter) {}  // This is a constructor
};

class Class2 {
public:
  int i;
  void function(int parameter) {i = parameter;} // This is a init function
};

and

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Class1 object1(10);
Class2 object2;
object2.function(10);
</code>
<code>Class1 object1(10); Class2 object2; object2.function(10); </code>
Class1 object1(10);
Class2 object2;
object2.function(10);

The value of i will be the same: object1.i == object2.i but the purpose of the constructor is NOT to save typing.

  • as Joel said in a comment , with the constructor it’s mandatory to assign a value to i, while with the init function you could use i without a previous explicit assignment.

    In properly designed OO-code the constructor is responsible for establishing the class invariants (here i should be a private data member). Without a constructor each use of the class must first ensure that the object has been properly built.

    You cannot re-initialize an object of a class using its constructor (*) but you can use operator=.

  • const objects can be created only via constructor:

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code>const Class1 object1(10);
    </code>
    <code>const Class1 object1(10); </code>
    const Class1 object1(10);
    

    you cannot write:

    Plain text
    Copy to clipboard
    Open code in new window
    EnlighterJS 3 Syntax Highlighter
    <code>const Class2 object; // error (uninitialized const object)
    object2.function(10); // error (attempt to change the const object)
    </code>
    <code>const Class2 object; // error (uninitialized const object) object2.function(10); // error (attempt to change the const object) </code>
    const Class2 object;   // error (uninitialized const object)
    object2.function(10);  // error (attempt to change the const object)
    
  • constructor is always more or equally efficient as having code outside

  • many other reasons (see references).

Technicalities

  • it’s possible to re-initialize an object of a class using its constructor via placement new. Of course it’s ugly beyond description.
  • there are reasons to prefer init functions. Init functions are able to call virtual functions on the object. This doesn’t always work in a constructor, because in the constructor of the base class, the derived class part of the object “doesn’t exist yet”, and in particular you can’t access virtual functions defined in the derived class. Instead, the base class version of the function is called, if defined (see also Why use an initialization method instead of a constructor?).

References

  • C++ – Constructor or Initialize Method to Startup
  • Arguments against “initialize()” method instead of constructors

2

I believe I’ve found the answer at cplusplus.com

It looks like I was correct, but that I should also keep in mind that the ‘constructor’ is only called at creation of the object and can never be called again per that object, whereas a function within the class can be called repeatedly.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật