如何初始化 CArray<CClass*> 的默认值具有空 CArray 的函数参数?
我知道我可以用 std::vector
做得更好,但是我正在搞乱的应用程序已经在很多相关的方面有一堆 CArray
参数函数...我暂时不会全部更改!
I know I could do this better with std::vector
, but the application I am messing with, has already a bunch of CArray
parameters on a lot of related functions ... and I will not change them all at the moment!
我只是想定义一个空的 CArray
- pointers 到 CClass
的数组,所以问题 不能在 CClass
构造函数上――作为函数参数的默认值.
I simply want to define an empty CArray<CClass*>
― array of pointers to CClass
, so the problem can not be on the CClass
constructor ― as the default value of a function parameter.
方法 1
如果我尝试使用 赋值运算符 和 默认构造函数:
If I try to do it with assignment operator and default constructor:
void Function(CArray<CClass*> parameter = CArray<CClass*>());
我得到错误:
1>C:Program Files (x86)Microsoft Visual Studio 10.0VCatlmfcincludeafxTempl.h(262): error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject'
1> C:Program Files (x86)Microsoft Visual Studio 10.0VCatlmfcincludeafx.h(535) : see declaration of 'CObject::CObject'
1> C:Program Files (x86)Microsoft Visual Studio 10.0VCatlmfcincludeafx.h(510) : see declaration of 'CObject'
1> This diagnostic occurred in the compiler generated function 'CArray<TYPE>::CArray(const CArray<TYPE> &)'
1> with
1> [
1> TYPE=CClass *
1> ]
方法 2
我也尝试了复制构造函数:
void Function(CArray<Class*> parameter(CArray<CClass*>()));
我得到了错误:
>File.cpp(line X): error C2664: 'FunctionClass::Function' : cannot convert parameter 1 from 'CArray<TYPE>' to 'CArray<TYPE> (__cdecl *)(CArray<TYPE> (__cdecl *)(void))'
1> with
1> [
1> TYPE=CClass*
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
- 第 X 行:包含向 Function 提供参数的调用,如图所示:
pFunctionClass->Function(parameter);
- Y行包含
Function
实现头,如图:`void CFunctionClass::Function(CArray parameter) - line Y contains the
Function
implementation header, as shown: `void CFunctionClass::Function(CArray parameter) - 第 Z 行:包含对
Function
的调用,但未提供参数,如图所示:pClassFunction->Function();
- line Z: contains a call to
Function
without supplying it parameters, as shown:pClassFunction->Function();
1>CFunctionClass.cpp(Y 行):错误 C2511:'void CFunctionClass::Function(CArray)':在 'CShoePlaceDoc' 中找不到重载的成员函数1> 与1> [1> 类型=C类*1>]1> FunctionClass.h(line A) : 见 'CFunctionClass' 的声明
1>CFunctionClass.cpp(line Y): error C2511: 'void CFunctionClass::Function(CArray)' : overloaded member function not found in 'CShoePlaceDoc' 1> with 1> [ 1> TYPE=CClass* 1> ] 1> FunctionClass.h(line A) : see declaration of 'CFunctionClass'
1>File.cpp(Z 行):错误 C2660:'CClassFunction::Function':函数不接受 0 个参数
1>File.cpp(line Z): error C2660: 'CClassFunction::Function' : function does not take 0 arguments
该方法不起作用,但它得到了一个结论:不可能使用复制构造函数来为函数参数分配默认值.
The approach didn't work, but it got its way towards a conclusion: It is not possible to use a copy-constructor for assigning a default value for a function parameter.
方法 3
如果我尝试使用 lambda:
void Function(CArray<CClass*> parameter = [] () -> CArray<CClass*>{ return CArray<CClass*> (); } );
,那么我会得到这两个错误的多个输出:
, then I will get multiple outputs of these two errors:
1>FunctionClass.h(line A): error C2440: 'default argument' : cannot convert from '`anonymous-namespace'::<lambda2>' to 'CArray<TYPE>'
1> with
1> [
1> TYPE=CClass*
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>FunctionClass.h(line B): fatal error C1903: unable to recover from previous error(s); stopping compilation
- A 行:方法声明
- B行:关闭包含
Function
方法的FunctionClass
类的}
问题的根源
问题的根源似乎是CArray
是一个直接派生自CObject
的类,将赋值运算符声明为私有:
The root cause of the problem seems to be the fact that CArray
is a class directly derived from CObject
, which declares the assignment operator as private:
class AFX_NOVTABLE CObject
{
//...
private:
CObject(const CObject& objectSrc); // no implementation
void operator=(const CObject& objectSrc); // no implementation
//...
}
那么,如何提供一个空数组作为参数的默认值?
So, how can I supply an empty array as default value for the parameter?
推荐答案
有了这个声明
void Function(CArray<CClass*> parameter /*...*/);
你不能.如您所见,调用此函数将调用 CObject
的私有复制构造函数.
You can't. Calling this function will invoke the private copy constructor of CObject
as you have noticed.
你可以做的是在你的类中添加一个 static CArray<CClass*>
的对象,并使用对它的引用来初始化函数.这样它将为空(只要您不填充它...),您可以执行 .IsEmpty()
检查一下.
What you could do, is to add a object of static CArray<CClass*>
in your class and initialize the function with a reference to it. This way it will be empty (as long as you do not populate it...) and you can perform a .IsEmpty()
check on it.
private:
static CArray<CClass*> myObj;
//...
void Function(CArray<CClass*> ¶meter = myObj);
或者将其初始化为0.这样你只需通过if (parameter)
或if (NULL == parameter)
来检查它.
Or initialize it to 0. This way you simply check it by if (parameter)
or if (NULL == parameter)
.
void Function(CArray<CClass*> *parameter = NULL);
相关文章