我正在尝试编译这段非常简单的代码

class myList
{
public:
    std::vector<std::string> vec;
    class Items
    {
    public:
        void Add(std::string str)
        {
            myList::vec.push_back(str);
        };
    }items;
};

int main()
{
    myList newList;
    newList.items.Add("A");
}

如果能够在不创建更多需要或过于复杂的东西的情况下完成这项工作,我该怎么办...

有帮助吗?

解决方案

添加几个构造函数和指向父类的指针。

#include <string>
#include <vector>
class myList
{
public:
    std::vector<std::string> vec;
    myList(): items(this) {} // Added
    class Items
    {
    public:
        Items(myList *ml): self(ml) {}  // Added
        void Add(std::string str)
        {
                self->vec.push_back(str); // Changed
        };
        myList *self; //Added
    }items;
};

int main()
{
    myList newList;
    newList.items.Add("A");
}

您需要myList()构造函数,因此它使用内部类成员变量的实例注册自身的实例。然后,您需要Items构造函数来存储指向外部myList类实例的指针。最后在Add方法中,您需要在存储的myList实例中引用vec。

正如Catskul指出的那样,Item构造函数实际上不能对它接收的myList指针做任何事情。我还想说,尽管这个答案更接近原始意图,但steveth45的答案更接近你想要在真实节目中做的事情。

其他提示

这样您就不会直接暴露您的班级成员。您的示例似乎过度架构了一下。为什么将std :: vector放入类中然后将其公开为public?

class myList
{
private:
    std::vector<std::string> vec;
public:
    void Add(std::string str)
    {
        vec.push_back(str);
    };
};

int main()
{
    myList newList;
    newList.Add("A");
}

与Java不同,C ++中的内部对象无法访问外部“this”指针......如果您考虑它,可能会出现无法引用的情况。

Richard Quirk的解决方案是您可以在C ++中获得的最接近的解决方案

内部类只与名称相关。你不能像那样引用基类中的向量。

您需要将向量移动到内部类或存储对它的引用。

虽然这篇文章已有几年历史了但我可能能够添加一些有用的东西。虽然我会说原始帖子中的类设计看起来不那么好,但有时候嵌入式类能够访问包含类是有用的。这可以很容易地完成,而无需存储额外的指针。以下是一个例子。它应该工作,因为我从一些现有的代码,并改变了一些名称。关键是EmbeddorOf宏。像魅力一样。

//////////////////// .h file //////////////////////// /

struct IReferenceCounted
{
    virtual unsigned long AddRef() = 0;
    virtual unsigned long Release() = 0;
};

struct IFoo : public IReferenceCounted
{
};

class Foo : public IFoo
{
public:
    static IFoo* Create();
    static IFoo* Create(IReferenceCounted* outer, IReferenceCounted** inner);

private:
    Foo();
    Foo(IReferenceCounted* outer);
    ~Foo();

    // IReferenceCounted

    unsigned long AddRef();
    unsigned long Release();

private:
    struct EIReferenceCounted : IReferenceCounted
    {
        // IReferenceCounted

        unsigned long AddRef();
        unsigned long Release();
    } _inner;

    unsigned long _refs;
    IReferenceCounted* _outer;
};

//////////////// .cpp file /////////////////

#include <stdio.h>
#include <stddef.h>
#include "Foo.h"

#define EmbeddorOf(class, member, this) \
    (class *) ((char *) this - offsetof(class, member))

// Foo

Foo::Foo() : _refs(1), _outer(&this->_inner)
{
}

Foo::Foo(IReferenceCounted* outer) : _refs(1), _outer(outer)
{
}

Foo::~Foo()
{
    printf("Foo::~Foo()\n");
}

IFoo* Foo::Create()
{
    return new Foo();
}

IFoo* Foo::Create(IReferenceCounted* outer, IReferenceCounted** inner)
{
    Foo* foo = new Foo(outer);
    *inner = &foo->_inner;
    return (IFoo*) foo;
}

// IReferenceCounted

unsigned long Foo::AddRef()
{
    printf("Foo::AddRef()\n");
    return this->_outer->AddRef();
}

unsigned long Foo::Release()
{
    printf("Foo::Release()\n");
    return this->_outer->Release();
}

// Inner IReferenceCounted

unsigned long Foo::EIReferenceCounted::AddRef()
{
    Foo* pThis = EmbeddorOf(Foo, _inner, this);
    return ++pThis->_refs;
}

unsigned long Foo::EIReferenceCounted::Release()
{
    Foo* pThis = EmbeddorOf(Foo, _inner, this);
    unsigned long refs = --pThis->_refs;
    if (refs == 0)
        {

        // Artifically increment so that we won't try to destroy multiple
        // times in the event that our destructor causes AddRef()'s or
        // Releases().

        pThis->_refs = 1;
        delete pThis;
        }
    return refs;
}

尼克

您可以通过以下构造简化:

typedef std::vector<std::string> myList;

你为什么不直接使用STL载体? 通过这种方式,您可以使用所有标准算法 数据

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top