在 C++如何创建类实例并调用结构体或什么都不调用?

发布于 2024-09-07 21:22:53 字数 637 浏览 8 评论 0原文

我正在创建一个类。

此类将用户首选项存储在结构中。

创建类的实例时,我希望客户端可以选择创建一个没有传入首选项或传入首选项结构的实例。

我可以使用指针来完成此操作,但我想知道如何做到这一点通过引用将首选项结构传递到类中。

无论哪种方式,一旦类收到首选项,它就会制作一个副本供自己使用。

这是指针的样子

struct preferences {};
class Useful 
{
public:
    Useful(preferences const * = NULL);
...
}

...

int main() 
{
   preferences * testPrefs;
   ...
   Useful testClass(testPrefs);
   // or if no prefs: Useful testClass;
   ...
}

那么,在创建默认值没有传入结构的类的实例时,如何通过引用传入首选项结构呢?这是我坚持的路线,因为 NULL 和 *NULL 都不起作用:

class Useful 
{
public:
    Useful(preferences & = ???????);

I'm creating a Class.

This class stores user preferences in a Struct.

When creating an instance of the class, I want the client to have the option of creating an instance with no preferences passed in or with a preferences struct passed in.

I can do this with pointers, but I wanted to know how I could do it by passing the preferences struct into the class by reference.

Either way, once the class receives the preferences it makes a copy for its own use.

Here's what it looks like with pointers

struct preferences {};
class Useful 
{
public:
    Useful(preferences const * = NULL);
...
}

...

int main() 
{
   preferences * testPrefs;
   ...
   Useful testClass(testPrefs);
   // or if no prefs: Useful testClass;
   ...
}

So how would you pass the preferences struct in by reference when creating an instance of the class with a default value of no struct passed in? This is the line I'm stuck on, since neither NULL nor *NULL will work:

class Useful 
{
public:
    Useful(preferences & = ???????);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

少女净妖师 2024-09-14 21:22:56

创建填充默认值的 preferences 结构的静态实例,并将其用作默认参数。或者用“魔法”值填充它,表明它是一个特殊的保留值。

Create a static instance of the preferences struct with default values filled in, and use that as the default parameter. Or fill it with "magic" values that indicate it as a special reserved value.

战皆罪 2024-09-14 21:22:56

您可以简单地重载构造函数:

struct preferences {};
class Useful 
{
public:
    Useful(preferences&);
    Useful(); // In here, do whatever you would have done 
              // if preferences were NULL in the pointer example
}

这本质上就是带有指针和默认参数的代码的第一个版本在幕后所做的事情。

You could simply overload the constructor:

struct preferences {};
class Useful 
{
public:
    Useful(preferences&);
    Useful(); // In here, do whatever you would have done 
              // if preferences were NULL in the pointer example
}

That's essentially what the first version of the code with the pointer and default argument is doing anyway, behind the scenes.

↙厌世 2024-09-14 21:22:56

您必须注意谁将成为“首选项”指针的所有者,如果您转移所有权,则类必须在析构函数中删除它,如果没有,调用者必须销毁它。

因为这可能会给您带来很多麻烦,所以我建议使用参考传递参数。

正如 Kate Gregory 所说,您必须定义两个构造函数,在第二个构造函数中,您必须将首选项复制到您自己的实例,至少该首选项只有一个且静态集。

使用独特且静态的“首选项”,您将遇到代码的其他部分修改它并改变代码工作方式的麻烦。

You must take care about who will be the owner of the "preferences" pointer, if you transfer the ownership the class must delete it in the destructor, if not, the caller must destroy it.

because this could give you lot of headaches I suggest use the reference pass parameters.

and as say Kate Gregory, you must define two constructors, in the 2nd you must copy the preferences to your own instance, at least that preference are only one and static set.

with an unique and static "preferences" you will have the trouble that other section of the code modified it and alter how your code works.

为人所爱 2024-09-14 21:22:54
Useful(const preferences& p = preferences(...));
Useful(const preferences& p = preferences(...));
绅士风度i 2024-09-14 21:22:54

您指出指针相对于引用的优势,以及指针如何适合您的情况,然后宣布您不想使用它们。

你仍然可以获得你想要的。为您的构造函数编写两个重载。一个接受引用,另一个不接受参数,并执行另一个构造函数在获得空指针时执行的操作。

You point out the advantage of pointers over references, and how well pointers fit your situation, then announce you don't want to use them.

You can still get what you want. Write two overloads for your constructor. One takes a reference, the other takes no parameters and does what the other constructor did when it got a null pointer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文