C++菜鸟问题:指针和重载[]

发布于 2024-09-10 02:26:16 字数 1966 浏览 9 评论 0原文

我已经盯着这个看了有一段时间了,但没有走多远。 FruitBasketFactory、FruitBasket 和 Fruit 是我正在使用的 API 中的三个类。我的目标是制作一个水果篮,然后取回水果。根据FruitBasketFactory.hpp:

const FruitBasket* getFruitBasket() const;

然后在FruitBasket.hpp中

size_t getNumFruits() const;
const Fruit& operator[](size_t index) const;

所以这是我的初始代码:

FruitBasketFactory fruitBasketFactory;
//since I want an object rather than a pointer I attempt to dereference the pointer
const FruitBasket fruitBasket = *(fruitBasketFactory.getFruitBasket());

但是这里我收到错误“错误C2248:'FruitBasket :: FruitBasket':无法访问类'FruitBasket'中声明的私有成员“。这不应该起作用吗?

所以,好吧...我重新编写了我的代码。

FruitBasketFactory fruitBasketFactory;
const FruitBasket* fruitBasket = fruitBasketFactory.getFruitBasket();

if(fruitBasket->getNumFruits() > 0) {
    //using (*fruitBasket)[0] seems silly
    const Fruit fruit = (*fruitBasket)[0];
}

并且失败“错误C2248:'Fruit::Fruit':无法访问类'Fruit'中声明的私有成员”

因此,另一次返工

FruitBasketFactory fruitBasketFactory;
const FruitBasket* fruitBasket = fruitBasketFactory.getFruitBasket();

if(fruitBasket->getNumFruits() > 0) {
    //this is just ludicrous ... I'm doing something wrong
    const Fruit* fruit = &(fruitBasket->operator[](0));
}

就像这段代码一样愚蠢看起来,它确实有效。但为什么我只能做我认为应该是最明显的事情呢?

FruitBasketFactory fruitBasketFactory;
const FruitBasket fruitBasket = *(fruitBasketFactory.getFruitBasket());

if(fruitBasket.getNumFruits() > 0) {
    const Fruit fruit = fruitBasket[0];
}

解决方案:

FruitBasket 和 Fruit 的复制构造函数确实被阻止。我可以通过创建引用来绕过调用它们,如下所示:

FruitBasketFactory fruitBasketFactory;
const FruitBasket& fruitBasket = *(fruitBasketFactory.getFruitBasket());

if(fruitBasket.getNumFruits() > 0) {
    const Fruit& fruit = fruitBasket[0];
}

I've been staring at this for a while and not getting very far. FruitBasketFactory, FruitBasket, and Fruit are three classes from an API I'm using. My goal is to make a fruit basket and then retrieve the fruit. According to the FruitBasketFactory.hpp:

const FruitBasket* getFruitBasket() const;

and then in the FruitBasket.hpp

size_t getNumFruits() const;
const Fruit& operator[](size_t index) const;

So here's my initial code:

FruitBasketFactory fruitBasketFactory;
//since I want an object rather than a pointer I attempt to dereference the pointer
const FruitBasket fruitBasket = *(fruitBasketFactory.getFruitBasket());

But here I get an error "error C2248: 'FruitBasket::FruitBasket' : cannot access private member declared in class 'FruitBasket'". Shouldn't this work?

So, fine... I rework my code.

FruitBasketFactory fruitBasketFactory;
const FruitBasket* fruitBasket = fruitBasketFactory.getFruitBasket();

if(fruitBasket->getNumFruits() > 0) {
    //using (*fruitBasket)[0] seems silly
    const Fruit fruit = (*fruitBasket)[0];
}

And fail: "error C2248: 'Fruit::Fruit' : cannot access private member declared in class 'Fruit'"

So, another rework

FruitBasketFactory fruitBasketFactory;
const FruitBasket* fruitBasket = fruitBasketFactory.getFruitBasket();

if(fruitBasket->getNumFruits() > 0) {
    //this is just ludicrous ... I'm doing something wrong
    const Fruit* fruit = &(fruitBasket->operator[](0));
}

As goofy as this piece of code looks, it actually works. But why can I just do what I think should be the most obvious thing?

FruitBasketFactory fruitBasketFactory;
const FruitBasket fruitBasket = *(fruitBasketFactory.getFruitBasket());

if(fruitBasket.getNumFruits() > 0) {
    const Fruit fruit = fruitBasket[0];
}

Resolution:

The copy constructors were indeed blocked for both FruitBasket and Fruit. I was able to get around calling them by creating references as follows:

FruitBasketFactory fruitBasketFactory;
const FruitBasket& fruitBasket = *(fruitBasketFactory.getFruitBasket());

if(fruitBasket.getNumFruits() > 0) {
    const Fruit& fruit = fruitBasket[0];
}

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

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

发布评论

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

评论(1

没︽人懂的悲伤 2024-09-17 02:26:16

但这里我收到错误错误C2248:'FruitBasket::FruitBasket':无法访问类'FruitBasket'中声明的私有成员

FruitBasket 的复制构造函数无法访问。您是否已将此类的复制构造函数声明为私有或受保护?这个类是否有任何不可复制的基类或成员(即没有可公开访问的复制构造函数)?

为了回答你的最后一个问题,

const Fruit* fruit = &(fruitBasket->operator[](0));

可以这样写:

const Fruit* fruit = &(*fruitBasket)[0];

必须取消引用指针,以便下标运算符应用于类类型 FruitBasket 对象,而不是指向水果篮

如果应用指针的下标,则会使用内置下标运算符,并将指针视为指向 FruitBasket 对象数组的指针。

But here I get an error error C2248: 'FruitBasket::FruitBasket' : cannot access private member declared in class 'FruitBasket'.

The copy constructor for FruitBasket is inaccessible. Have you declared the copy constructor for this class as private or protected? Does this class have any bases or members that are not copyable (i.e., that do not have publicly accessible copy constructors)?

To answer your last question, this:

const Fruit* fruit = &(fruitBasket->operator[](0));

can be much more reasonably written as this:

const Fruit* fruit = &(*fruitBasket)[0];

The pointer must be dereferenced so that the subscript operator is applied to the class-type FruitBasket object and not to the pointer to the FruitBasket.

If you apply the subscript of the pointer, the built-in subscript operator is used and the pointer is treated as a pointer to an array of FruitBasket objects.

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