To create a constructor for a C++ structure?

Asked 2 years ago, Updated 2 years ago, 80 views

How do I create a construct in C++ structure? I want to define the constructor, but I don't know the grammar

c++ struct constructor

2022-09-22 13:58

1 Answers

C and C++ structures look similar, but they are actually different data structures.

In C++, class and structure are

Only the is different, and the other functions are the same.

Therefore, when defining the constructor of a structure in C++, you can write the name of structure just like class.

Example

:

.
struct foo{
    int a, b;
    foo() : a(0), b(0) {
        cout << "foo made" << endl;
    }
};


int main(){
    struct foo f1;
}


2022-09-22 13:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.