c++ - How can I initialize the default value of a CArray<CClass*> function parameter with an empty CArray? -
i know better std::vector, application messing with, has bunch of carray parameters on lot of related functions ... , not change them @ moment!
i want define empty carray<cclass*> — array of pointers cclass, problem can not be on cclass constructor — default value of function parameter.
approach 1
if try assignment operator , default constructor:
void function(carray<cclass*> parameter = carray<cclass*>()); i error:
1>c:\program files (x86)\microsoft visual studio 10.0\vc\atlmfc\include\afxtempl.h(262): error c2248: 'cobject::cobject' : cannot access private member declared in class 'cobject' 1> c:\program files (x86)\microsoft visual studio 10.0\vc\atlmfc\include\afx.h(535) : see declaration of 'cobject::cobject' 1> c:\program files (x86)\microsoft visual studio 10.0\vc\atlmfc\include\afx.h(510) : see declaration of 'cobject' 1> diagnostic occurred in compiler generated function 'carray<type>::carray(const carray<type> &)' 1> 1> [ 1> type=cclass * 1> ] approach 2
i tried copy constructor:
void function(carray<class*> parameter(carray<cclass*>())); i got errors:
>file.cpp(line x): error c2664: 'functionclass::function' : cannot convert parameter 1 'carray<type>' 'carray<type> (__cdecl *)(carray<type> (__cdecl *)(void))' 1> 1> [ 1> type=cclass* 1> ] 1> no user-defined-conversion operator available can perform conversion, or operator cannot called - line x: contains call supplying parameter function, shown:
pfunctionclass->function(parameter);
1>cfunctionclass.cpp(line y): error c2511: 'void cfunctionclass::function(carray)' : overloaded member function not found in 'cshoeplacedoc' 1> 1> [ 1> type=cclass* 1> ] 1> functionclass.h(line a) : see declaration of 'cfunctionclass'
- line y contains
functionimplementation header, shown: `void cfunctionclass::function(carray parameter)
1>file.cpp(line z): error c2660: 'cclassfunction::function' : function not take 0 arguments
- line z: contains call
functionwithout supplying parameters, shown:pclassfunction->function();
the approach didn't work, got way towards conclusion: not possible use copy-constructor assigning default value function parameter.
approach 3
and if try lambda:
void function(carray<cclass*> parameter = [] () -> carray<cclass*>{ return carray<cclass*> (); } ); , multiple outputs of these 2 errors:
1>functionclass.h(line a): error c2440: 'default argument' : cannot convert '`anonymous-namespace'::<lambda2>' 'carray<type>' 1> 1> [ 1> type=cclass* 1> ] 1> no user-defined-conversion operator available can perform conversion, or operator cannot called 1>functionclass.h(line b): fatal error c1903: unable recover previous error(s); stopping compilation - line a: method declaration
- line b: closing
}offunctionclassclass containsfunctionmethod
origin of problem
the root cause of problem seems fact carray class directly derived cobject, declares assignment operator private:
class afx_novtable cobject { //... private: cobject(const cobject& objectsrc); // no implementation void operator=(const cobject& objectsrc); // no implementation //... } so, how can supply empty array default value parameter?
with declaration
void function(carray<cclass*> parameter /*...*/); you can't. calling function invoke private copy constructor of cobject have noticed.
what do, add object of static carray<cclass*> in class , initialize function reference it. way empty (as long not populate it...) , can perform .isempty() check on it.
private: static carray<cclass*> myobj; //... void function(carray<cclass*> ¶meter = myobj); or initialize 0. way check if (parameter) or if (null == parameter).
void function(carray<cclass*> *parameter = null);
Comments
Post a Comment