Accessing a member of an object void pointer c++

时间:2016-02-12 22:02:17

标签: c++ pointers void-pointers

I have an assignment where I have to use a linked list of node with void* as the data. I would be filling the nodes with an object. I want to know some way of accessing the members of the object after it is in the linked list, other than casting it to the class. Is it possible? Also, here's a chunk of my code just in case it helps clarify my question.

<IfModule mod_rewrite.c>

# Set SERVER_ENV=production if HTTP_HOST matches my production URL
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
RewriteRule .* - [E=SERVER_ENV:production]

# Force www. if SERVER_ENV is production
RewriteCond %{ENV:SERVER_ENV} ^production$
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Force https if SERVER_ENV is production
RewriteCond %{ENV:SERVER_ENV} ^production$
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Set SERVER_ENV=local if HTTP_HOST matches my local URL
RewriteCond %{HTTP_HOST} ^www-local\.mysite\.local$ [NC]
RewriteRule .* - [E=SERVER_ENV:local]

</IfModule>

I want to be able to call the PrintToConsole function after it is in a void*.

4 个答案:

答案 0 :(得分:1)

You cannot work with the pointee of a class MyAdapter extends ArrayAdapter<String> { View getView(int position, View convertView, ViewGroup parent) { View ret = super.getView(position, convertView, parent); if (position == 0) { ret.setBackground(red); } else { ret.setBackground(white); } return ret; } } without first casting it first. If it points at a void*, for example, you could do something like this:

Star

That said, in C++, it's pretty unusual to store things like this. You're much better off using a template class so that you get back the type information you need.

答案 1 :(得分:0)

You should cast it to the class. But if you really don't want to, you can use the offsetof macro:

The macro offsetof expands to a constant of type std::size_t, the value of which is the offset, in bytes, from the beginning of an object of specified type to its specified member, including padding if any. If type is not a standard layout type, the behavior is undefined. If member is a static member or a member function, the behavior is undefined.

But you should just cast it to the class.

EDIT: Ah, I see you want to access a method of the class. That's not possible. You should cast it to the class.

答案 2 :(得分:0)

No. You have to cast it to the appropriate object.

I would question the reason on using void pointers.

I would also suggest a dynamic cast might be better

答案 3 :(得分:0)

由于这是一项作业,您可能最好向您的老师/导师询问他们在C ++中使用void*类型的意图; void*类型本身并不是,但还有其他方法可以在保持语言一致性的同时实现类似的结果。

直接回答:

  

我想知道在链接列表中访问对象成员之后的某种方式,而不是将其强制转换为类。有可能吗?

是的,但是没有使用void*成语。使用您的代码作为示例,您确实必须转换为适当的类型并且某些指向的类型是兼容的,如果您保留void*,例如:

struct Node
{
    void* data_;
    Node* next_;

    Node()
    {
        data_ = 0;
        next_ = 0;
    }
};

class Star
{
    public:
        void PrintToConsole() {} // empty to avoid not-defined errors
};

int main() {
    Node n;
    n.data_ = new int(42);
    static_cast<Star*>(n.data_)->PrintToConsole(); // this will compile fine, but what happens here is undefined
    delete static_cast<int*>(n.data_); // can't delete void*, have to cast
    return 0;
}

同样,由于这是一个作业,你的教授可能只是想教指针和演员或类型系统你可能还没有学过C ++模板,但是既然你问过,这里是你的代码使用模板: / p>

template < typename T >
struct Node
{
    T data_;
    Node* next_;

    // use member init list to construct default T object
    Node() : data_(), next_(0)
    {
    }
};

class Star
{
    public:
        void PrintToConsole() {} // empty to avoid not-defined errors
};

int main() {
    Node<Star*> n;
    n.data_ = new Star();
    n.data_->PrintToConsole(); // OK
    delete n.data_; // no cast needed since data_ is a Star* type

    Node<int*> n2;
    n2.data_ = new Star(); // compiler error (data_ is of type int* not Star*)
    n2.data_->PrintToConsole(); // compiler error (int has no member named PrintToConsole)
    delete n.data_;
    return 0;
}

这只是一个简单的例子来说明你的要求,如果你对这个话题感到困惑,最好还是请教老师澄清一下。

希望可以提供帮助。