I'm creating an interface wrapper for a class. The member within the class is a reference(to avoid copying the large structure). If I create a private constructor, what is the best way to initialize that reference to appease the compiler?
我正在为类创建一个接口包装器。类中的成员是一个引用(以避免复制大型结构)。如果我创建一个私有构造函数,那么初始化该引用以安抚编译器的最佳方法是什么?
struct InterfaceWrapper {
InterfaceWrapper( SomeHugeStructure& src ):m_internal(src){};
int someElement(void) const { return m_internal.someElement; };
private:
InterfaceWrapper(){} // initialize m_internal
SomeHugeStructure& m_internal;
};
4 个解决方案
#1
4
As others have mentioned, if your purpose is to prevent others from calling the default constructor, then you don't want to provide a body at all, and declaring it is unnecessary since you have another constructor and the compiler won't generate it for you.
正如其他人所提到的,如果你的目的是阻止其他人调用默认构造函数,那么你根本不想提供一个正文,并且声明它是不必要的,因为你有另一个构造函数而且编译器不会为它生成它您。
If the purpose is to limit access to friends of the class, then you would probably be best having a pointer (prefereably a smart pointer) member, and setting it to NULL.
如果目的是限制对类的朋友的访问,那么你可能最好有一个指针(最好是智能指针)成员,并将其设置为NULL。
--
Also, I'm not sure why you made this a struct rather than a class. In general making something a struct makes sense when you're exposing data members as public, similar to C. In this case, it looks like you have a conventional class, in which case the C++ convention would be to make it a class.
另外,我不确定你为什么把它变成结构而不是类。一般来说,当你将数据成员暴露为公共时,结构是有意义的,类似于C.在这种情况下,看起来你有一个传统的类,在这种情况下,C ++约定是使它成为一个类。
#2
4
If you really need to construct the class without having an element, a reference is not the way to go.
如果你真的需要在没有元素的情况下构造类,那么引用就不是了。
If your only reason to use a reference is to avoid copying, I'd suggest using a pointer. You can simply take the address of the passed reference in you regular constructor, and initialize it to NULL in the private constructor. When the object's lifetime ends the pointer will be a dangling pointer, but the same is true for references...
如果您使用引用的唯一原因是避免复制,我建议使用指针。您可以在常规构造函数中简单地获取传递的引用的地址,并在私有构造函数中将其初始化为NULL。当对象的生命周期结束时,指针将是一个悬空指针,但对于引用也是如此......
Alternatively, have the public constructor take a smart pointer and use such a smart pointer as member too, if you're concerned about lifetimes.
或者,让公共构造函数采用智能指针并使用这样的智能指针作为成员,如果你关心生命周期。
And of course, cope with the pointer possibly being NULL in the remainder of the class' code. (but you had to cope with the member possible being a stub in the reference case too, so that's not really an issue)
当然,在类代码的其余部分中处理可能为NULL的指针。 (但你必须应对成员可能在参考案例中作为存根,所以这不是一个真正的问题)
#3
2
If you are making the default ctor private to prevent anyone from using it, then just leave off the body:
如果您将默认的ctor设为私有以防止任何人使用它,那么只需离开身体:
private:
InterfaceWrapper();
SomeHugeStructure& m_internal;
The compiler will think it's defined elsewhere, and the linker won't mind that it's not unless someone tries to use it.
编译器会认为它是在别处定义的,并且链接器不会介意它除非有人试图使用它。
#4
1
What is the point of the private ctor? If m_internal is not referencing a valid object, how can it be useful?
私人监管有什么意义?如果m_internal没有引用有效对象,它如何有用呢?
Also the m_internal-> won't compile. That is pointer syntax, not ref.
m_internal->也不会编译。那是指针语法,而不是ref。
#1
4
As others have mentioned, if your purpose is to prevent others from calling the default constructor, then you don't want to provide a body at all, and declaring it is unnecessary since you have another constructor and the compiler won't generate it for you.
正如其他人所提到的,如果你的目的是阻止其他人调用默认构造函数,那么你根本不想提供一个正文,并且声明它是不必要的,因为你有另一个构造函数而且编译器不会为它生成它您。
If the purpose is to limit access to friends of the class, then you would probably be best having a pointer (prefereably a smart pointer) member, and setting it to NULL.
如果目的是限制对类的朋友的访问,那么你可能最好有一个指针(最好是智能指针)成员,并将其设置为NULL。
--
Also, I'm not sure why you made this a struct rather than a class. In general making something a struct makes sense when you're exposing data members as public, similar to C. In this case, it looks like you have a conventional class, in which case the C++ convention would be to make it a class.
另外,我不确定你为什么把它变成结构而不是类。一般来说,当你将数据成员暴露为公共时,结构是有意义的,类似于C.在这种情况下,看起来你有一个传统的类,在这种情况下,C ++约定是使它成为一个类。
#2
4
If you really need to construct the class without having an element, a reference is not the way to go.
如果你真的需要在没有元素的情况下构造类,那么引用就不是了。
If your only reason to use a reference is to avoid copying, I'd suggest using a pointer. You can simply take the address of the passed reference in you regular constructor, and initialize it to NULL in the private constructor. When the object's lifetime ends the pointer will be a dangling pointer, but the same is true for references...
如果您使用引用的唯一原因是避免复制,我建议使用指针。您可以在常规构造函数中简单地获取传递的引用的地址,并在私有构造函数中将其初始化为NULL。当对象的生命周期结束时,指针将是一个悬空指针,但对于引用也是如此......
Alternatively, have the public constructor take a smart pointer and use such a smart pointer as member too, if you're concerned about lifetimes.
或者,让公共构造函数采用智能指针并使用这样的智能指针作为成员,如果你关心生命周期。
And of course, cope with the pointer possibly being NULL in the remainder of the class' code. (but you had to cope with the member possible being a stub in the reference case too, so that's not really an issue)
当然,在类代码的其余部分中处理可能为NULL的指针。 (但你必须应对成员可能在参考案例中作为存根,所以这不是一个真正的问题)
#3
2
If you are making the default ctor private to prevent anyone from using it, then just leave off the body:
如果您将默认的ctor设为私有以防止任何人使用它,那么只需离开身体:
private:
InterfaceWrapper();
SomeHugeStructure& m_internal;
The compiler will think it's defined elsewhere, and the linker won't mind that it's not unless someone tries to use it.
编译器会认为它是在别处定义的,并且链接器不会介意它除非有人试图使用它。
#4
1
What is the point of the private ctor? If m_internal is not referencing a valid object, how can it be useful?
私人监管有什么意义?如果m_internal没有引用有效对象,它如何有用呢?
Also the m_internal-> won't compile. That is pointer syntax, not ref.
m_internal->也不会编译。那是指针语法,而不是ref。