Is default constructor always required ??

Aug 24, 2010 at 12:20pm
Hi,

Should I define default constructors every time even if my used constuctor is always with arguments?

Any problems if I do not define it?

Thanks...

--
eye51
Aug 24, 2010 at 12:49pm
Without a default constructor, you'll find that you can't use your type with much of the STL.

Aug 24, 2010 at 2:03pm
You should always define the copy constructor and assignment operator unless you specifically want to disable copy or explicity want to use the defaults. Coplien calls the the cannonical form (along with an appropriate constructor and destructor).
Aug 24, 2010 at 2:46pm
I think you have it backwards.

You should declare the copy constructor and assignment operator private and leave them unimplemented if
you want to disable copyability and assignability. Otherwise you should declare and implement the copy
constructor and assignment operator (and also the destructor by the rule of three) only if the compiler-provided
member-wise copy versions are not sufficient.

Aug 24, 2010 at 2:49pm
No, any problems.
The constructor is not needed at all as well as the destructor while C++ inheritence feature is
not the case.
In this one the construct
base_to_derive:[access specifiers][virtuallity qualifires][...],[...[derived_type_name]]
{
derived_constructor_instance (parameters,...) : base_to_derive_constructor_from

(parameters,...)
{...;}
};

will never be got ridden of in its following classes.
Aug 24, 2010 at 4:00pm
Hi,

Thanks for all the replies. My problem is here in the example below:
If in _YourClass, I include its constructor, it gives compilation error like this:

initlistref.cpp: In constructor ‘_YourClass::_YourClass()’:
initlistref.cpp:16: error: uninitialized reference member ‘_YourClass::myclass’

I am using initialization list and passing vector by reference.

Does anybody have any idea why this error is coming?

///////////////////////////////////////////////////////////////////
#include <iostream>
#include <vector>
using namespace std;

class _MyClass
{
public:
_MyClass(){};
int variable;
};
typedef class _MyClass MyClass;

class _YourClass
{
public:
_YourClass() // this gives error ???
{
}
~_YourClass()
{
};
_YourClass(vector <MyClass> &_myclass_) : myclass(_myclass_)
{
myclass.resize(10);

cout << "myclass size : " << sizeof(myclass) << endl;
cout << "iter size : " << sizeof(iter) << endl;
int i=0;
for(iter = myclass.begin(); iter != myclass.end(); ++iter)
{
iter->variable = i * 10;
i++;
}


}
private:
vector <MyClass> &myclass;
vector <MyClass>::iterator iter;
};
typedef class _YourClass YourClass;

void MyFunction(vector <MyClass> &myclass)
{
myclass.resize(100);
}

int main(int argv, char **argc)
{
vector <MyClass> myclass;
cout << "myclass.size() : " << myclass.size() << endl;
YourClass *yourclass = new YourClass(myclass);
cout << "myclass.size() : " << myclass.size() << endl;
for(int i = 0; i < myclass.size(); i++)
cout << "myclass[i].variable : " << myclass[i].variable << endl;

MyFunction(myclass);
cout << "myclass.size() : " << myclass.size() << endl;
cout << "var : " << myclass[5].variable << endl;

return 0;
}

Thanks...

--
eye51
Aug 24, 2010 at 4:13pm
What you want to do whit this
vector <MyClass> &myclass;

That's an alias.
Aug 24, 2010 at 4:19pm
Hi,
Thanks for reply.

I want to change MyClass properties through YourClass. Hence I am passing MyClass by reference to YourClass constructor, taking its reference in private reference variable myclass and using it in other members of YourClass. If I want to use MyClass in other member functions of YourClass, I will have to take it in a local private variable and whatever changes I do with MyClass will change myclass variable in my main() function.

--
eye51
Aug 24, 2010 at 4:40pm
The point is that what an alias refer is set in the moment of construction.
That's why the compiler is kicking you out in the default constructor.

Also you cannot change that reference later, so maybe you want to use a pointer instead.
Aug 24, 2010 at 8:16pm
I want to change MyClass properties through YourClass


As I have correctly aquired formally the program will come to the desired end though,
let me draw your attention to the aid over the following link

http://www.cplusplus.com/doc/tutorial/inheritance/
Friends classes section.

Please note there is no other path to achieve it so that a private class member
could be accessed in public ones class changefully. Also such classes are uninheritable
by a constrictive reason of a cross reference despite of all the C++ flexibility.
Of course the advanced typecast will provide more aids down over a journey up to the
higher level storage steps.

regards
Topic archived. No new replies allowed.