How can I initialize the default value of a CArraylt;CClass*gt; function parameter with an empty CArray?(如何初始化 CArraylt;CClass*gt; 的默认值具有空 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);
这篇关于如何初始化 CArray<CClass*> 的默认值具有空 CArray 的函数参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何初始化 CArray<CClass*> 的默认值具有空 CArray 的函数参数?
基础教程推荐
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01