English 中文(简体)
在 C++ 中创建对象指针
原标题:Creating object pointers in C++
  • 时间:2012-05-25 18:36:55
  •  标签:
  • c++

我试图创造一个东西, 我想在开关箱里植入它, 但是它超出了我的知识范围。

我有这些建筑师:

cObj::cObj()
{
}

cObj::cObj(std::string filename)
{
    //...
}

所以,基本上,我想调用 遵循的方法, 创建一个指向对象的指针, 并把它放在我的开关大小写里:

void someThing() {
    cObj myObj();

    switch (someValue)
        case 0:
            myObj("/some/path");
            break;
        ...
}

我猜我的建筑师是错的 因为它没有真正工作。

我怎么能做到这一点?

最佳回答

当您在此行创建对象时

cObj myObj();

(btw. 您可能不想要这些括号。 您想要创建对象, 而不是声明函数) 。

you call the constructor. You can not call it again in switch statement. You could create a separate method:

cObj::cObj()
{
}

void cObj::SetFilename(const std::string& filename) {
// ...
}

并使用它这样:

void someThing() {
    cObj myObj;

    switch (someValue)
        case 0:
            myObj.SetFilename("/some/path");
            break;
        ...
}

我不知道你想做什么 但也许更好的办法是 先确定文件路径是什么 然后创建对象?

void someThing() {
    std::string filepath = "default/path";

    switch (someValue)
        case 0:
            filepath = "some/path";
            break;
        ...

    cObj myObj(flepath);
}

您也可以创建一个函数, 以决定要使用和返回对象的路径 :

问题回答

你不需要这个指针

void someThing() {
    cObj myObj; // Don t use parentheses for the default constructor.
                // What you had was a function declaration, not an object creation.

    switch (someValue) {
        case 0:
            myObj = cObj("/some/path");
            break;
        ...
}

如果您没有默认的构建器, 或者您不希望它被调用, 那么您可以使用一个指针, 最好是智能的 :

void someThing() {
    std::unique_ptr<cObj> myObj;

    switch (someValue) {
        case 0:
            myObj.reset(new cObj("/some/path"));
            break;
        ...
}

或者,正如 lmmmilewski 暗示的那样,你可以将决定因素推到函数中,然后返回对象:

cObj choose(someType someValue) {
    switch (someValue) {
        case 0:
            return cObj("/some/path");
        ...
}

void someThing() {
    cObj myObj(choose(someValue));
    ...
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签