Question: 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.

```
struct Node
{
    void* data_;
    Node* next_;

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

class Star
{
private:
    char name_[ENTRY_SZ];
    long temperature_;
    double luminosity_;
    double mass_;
    double radius_;

public:
    static char filePath_[ENTRY_SZ];

    Star(char* name);
    void SetTemperature(char* temp);
    void SetLuminosity(char* lum);
    void SetMass(char* mass);
    void SetRadius(char* rad);
    void PrintToConsole();
    void AppendToFile();
};

```

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

Answer: 
You cannot work with the pointee of a `void*` without first casting it first. If it points at a `Star`, for example, you could do something like this:

```
static_cast<Star*>(data_)->PrintToConsole(); // Haha, Star 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.
Since this is an assignment, you might be best to ask your teacher/mentor as to what their intents are for using a `void*` type in C++; `void*` types are not inherently *bad*, but there other ways of achieving similar results while maintaining language consistency.

To answer directly:

> 
> 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?
> 
> 
> 

Yes it is possible, but not using the `void*` idiom. Using your code as an example, you would indeed have to cast to the appropriate type and be **certain** the types pointed to are compatible if you keep the `void*`, example:

```
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;
}

```

Again, since this is an assignment, your professor is probably just trying to teach about pointers and casting or the type system and you probably haven't learned about C++ templates, but since you asked, here's your code using templates:

```
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;
}

```

This is just a simple example to illustrate what you were asking and it's still probably best to ask your teacher for more clarification if your confused on the topic.

Hope that can help.