相关疑难解决方法(0)

什么是C++中的 - >*运算符?

C++继续让我感到惊讶.今天我发现了 - >*运算符.它是可重载的,但我不知道如何调用它.我设法在课堂上重载它,但我不知道如何调用它.

struct B { int a; };

struct A
{
    typedef int (A::*a_func)(void);
    B *p;
    int a,b,c;
    A() { a=0; }
    A(int bb) { b=b; c=b; }
    int operator + (int a) { return 2; }
    int operator ->* (a_func a) { return 99; }
    int operator ->* (int a) { return 94; }
    int operator * (int a) { return 2; }
    B* operator -> () { return p; }


    int ff() { return 4; }
}; …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading operator-arrow-star

21
推荐指数
2
解决办法
2247
查看次数

这个star(*)符号在C++中的含义是什么? - 指向会员的指针

在我的项目中,MyClass.h文件中有几行

class MyClass{
public:
....

#ifndef __MAKECINT__
  std::vector<Status_e(MyClass::*)(TupleInfo & info)> m_processObjects; //!
#endif
....
}
//The Status_e is an enum type
//The TupleInfo is a struct
Run Code Online (Sandbox Code Playgroud)

*在上面的代码片段中无法理解这个用法.在MyClass.cxx文件中,m_processObjects用作:

for (unsigned int f = 0; f < m_processObjects.size(); ++f) {
    status = (this->*m_processObjects[f])( m_tuples[i] );
    if ( status == FAILURE ) {
      return EL::StatusCode::FAILURE;
    }
....
}
....
Run Code Online (Sandbox Code Playgroud)

我从来没有听说过这样的用法this->*blabla,但这段代码片段有效.那意味着什么?

c++ pointers

8
推荐指数
1
解决办法
425
查看次数

按名称获取属性

我有一个包含大约25个元素的结构定义

struct X { field 1; field 2; .. };    
Run Code Online (Sandbox Code Playgroud)

而我正在尝试用一些地图值填充它

Map<String,String> A    
Run Code Online (Sandbox Code Playgroud)

这样做n次似乎很烦人

X->xx = A["aaa"]    
Run Code Online (Sandbox Code Playgroud)

每次我想填写我的消息结构.

是否可以通过名称访问成员,例如

X->get_instance_of("xx").set(A["aaa"]);    
Run Code Online (Sandbox Code Playgroud)

把它放到一个循环中?

c++ struct getelementsbyname

6
推荐指数
2
解决办法
6630
查看次数

以类方法为参数的 C++ 模板

是否可以将类方法作为参数传递给模板?例如:

template<typename T, void(T::*TFun)()>
void call(T* x) {
    x->TFun();
}

struct Y {
    void foo();
};

int main() {
    Y y;
    call<Y,&Y::foo>(&y);  // should be the equivalent of y.foo()
}
Run Code Online (Sandbox Code Playgroud)

如果我尝试编译上述内容,我会得到:

main.cpp: In instantiation of void call(T*) [with T = Y; void (T::* TFun)() = &Y::foo]:
main.cpp:12:23:   required from here
main.cpp:4:5: error: struct Y has no member named TFun
     x->TFun();
     ^
Run Code Online (Sandbox Code Playgroud)

这可能吗,如果可能,语法是什么?

c++ templates

4
推荐指数
1
解决办法
1411
查看次数

在C++中使用不正确的向下转换访问基类成员

以下C++代码是否正确?

struct Base { int x; };
struct Derived : Base { int y; }
Base * b = new Base;
Derived * d = static_cast<Derived *>(b);
//below we access only d->x, but not d->y
std::cout << d->x;
Run Code Online (Sandbox Code Playgroud)

如果没有,究竟出了什么问题?C++标准对此有何看法?至少我没见过它曾经坠毁过.

c++ language-lawyer

2
推荐指数
1
解决办法
71
查看次数

C++运算符 - >*和.*

美好的一天,

我已经遇到这个问题,但我特别感兴趣的是"指向的对象成员..."作为上市型运营商在这里维基百科.

我从未在实际代码的上下文中看到过这个,所以这个概念对我来说有些深奥.

我的直觉说他们应该按如下方式使用:

struct A
{
    int *p;
};

int main()
{
    {
        A *a = new A();
        a->p = new int(0);
        // if this did compile, how would it be different from *a->p=5; ??
        a->*p = 5;
    }

    {
        A a;
        a.p = new int(0);
        // if this did compile, how would it be different from *a.p=5; ??
        a.*p = 5;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但这不会编译因为p未声明.(见例)

任何人都可以提供一个在C++中使用operator - >*和/或.*的真实示例吗?

c++ operators dereference member-access

0
推荐指数
1
解决办法
172
查看次数

使用 .*&amp; 访问基类成员(访问priority_queue容器)

我试图理解下面的代码

如何迭代priority_queue?

我认为,由于 HackedQueue 是从priority_queue 私有派生的,因此它可以访问其私有。所以,我假设*&HackedQueue::c返回基类对象的地址,并为 q 调用它。但尚不完全清楚,甚至更不清楚它如何成为有效的语法。


提到priority_queue,我想知道是否仍然没有更干净的解决方法。例如,此链接上的第四个 c'tor

priority_queue( const Compare& compare, Container&& cont );

https://en.cppreference.com/w/cpp/container/priority_queue/priority_queue

似乎提供了一个可以使用的容器,而不是作为只读输入。我在 Visual Studio 头文件中没有看到它,而且我不清楚&&.


相关的是,我不明白相反的问题,例如为什么我需要访问私有容器,它不是为此而设计的。那么,如何调试优先级队列,其中基本需求是打印其元素?


#include <queue>
#include <cstdlib>
#include <iostream>
using namespace std;

template <class T, class S, class C>
S& Container(priority_queue<T, S, C>& q) {
    struct HackedQueue : private priority_queue<T, S, C> {
        static S& Container(priority_queue<T, S, C>& q) {
            return q.*&HackedQueue::c;
        }
    };
    return HackedQueue::Container(q);
}

int main()
{
    priority_queue<int> pq;
    vector<int> &tasks = Container(pq); …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
263
查看次数