返回指针的方法,指向另一个标头中声明的数组对象,

发布于 2024-10-07 13:19:12 字数 1291 浏览 3 评论 0原文

我遇到了两个相互交织的问题。

  • 首先,我想要一个指向堆上对象的指针数组。 (在另一个标头中声明的对象)

  • 其次,我想让一个方法返回指向其中一个对象的指针。

我当前的代码是一些摸索的结果,并且会失败,因为我不能在没有完全声明它的情况下使用“bar”作为返回类型。但我看不出还有什么办法可以解决这个问题。我尝试使“getBar”成为指向函数的指针,但随后我不知道如何使其访问 **barArray 而不是成员方法。

任何帮助将不胜感激:D

我的代码:

foo.h

#ifndef FOO_H
#define FOO_H

//forward declaration
class bar;

class foo  
{  
    public:  
        //constructor
        foo(int x);  
        //method
        bar * getBar(int y);  
    private:  
        int howManyBars;
        bar **barArray;  
};

#endif

foo.cpp

#include "foo.h"
#include "bar.h"  

//constructor
foo::foo(int x)
{
    howManyBars = x;
    barArray = new bar *[howManyBars];

    for (int i=0; i < howManyBars ; i++)
    {
        barArray[i] = NULL; //set all pointers to NULL
    }
}

//method
bar * foo::getBar(int y)
{
    y = (y - 1);
    // if the pointer is null, make an object and return that
    if (barArray[y] == NULL)
    {
        barArray[y] = new bar();
    }
    return barArray[y];
}

bar.h

#ifndef BAR_H
#define BAR_H

#include <iostream>

class bar
{
    public:
        void test(){std::cout << "I'm alive!\n";};
};
#endif

I'm getting a little stuck with two entwined problems.

  • First, I want to have an array of pointers to objects on the heap. (objects that are declared in another header)

  • Second, then I want to have a method return a pointer to one of those objects.

My current code is the result of a bit of fumbling, and will fail because I can't use "bar" as a return type without fully declaring it. But I can't see how else to solve the problem. I tried to make "getBar" a pointer to a function, but then I don't know how to make it access **barArray without it being a member method.

Any help would be much appreciated :D

My code:

foo.h

#ifndef FOO_H
#define FOO_H

//forward declaration
class bar;

class foo  
{  
    public:  
        //constructor
        foo(int x);  
        //method
        bar * getBar(int y);  
    private:  
        int howManyBars;
        bar **barArray;  
};

#endif

foo.cpp

#include "foo.h"
#include "bar.h"  

//constructor
foo::foo(int x)
{
    howManyBars = x;
    barArray = new bar *[howManyBars];

    for (int i=0; i < howManyBars ; i++)
    {
        barArray[i] = NULL; //set all pointers to NULL
    }
}

//method
bar * foo::getBar(int y)
{
    y = (y - 1);
    // if the pointer is null, make an object and return that
    if (barArray[y] == NULL)
    {
        barArray[y] = new bar();
    }
    return barArray[y];
}

bar.h

#ifndef BAR_H
#define BAR_H

#include <iostream>

class bar
{
    public:
        void test(){std::cout << "I'm alive!\n";};
};
#endif

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

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

发布评论

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

评论(1

手心的温暖 2024-10-14 13:19:12

除了一些拼写错误之外,这编译得很好:

  1. 定义 bar 类后需要一个分号。
  2. bar * foo:getBar(int y)

应该是:

bar * foo::getBar(int y)

3.

bar[i] = NULL; //set all pointers to NULL

应该是:

barArray[i] = NULL; //set all pointers to NULL

Other than a few typos, this compiles fine:

  1. You need a semi-colon after defining the bar class.
  2. bar * foo:getBar(int y)

should be:

bar * foo::getBar(int y)

3.

bar[i] = NULL; //set all pointers to NULL

should be:

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