Sign in to follow this  

[C++] Accessing an object ouside scope

This topic is 3492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm just wondering if there's a way to point to an object ouside the scope of a class. I'll use a case example to try an define what I'm after Have two lists. One list is a bunch of information that may be used by different objects, lets say graphic data. each element in the list is a mesh. The second list is a list of active objects, some of those objects use the same graphics, so reference the meshes from the grahpics list. List #1: meshes List #2: objects Now your typical reference would be to figure out what mesh the object uses, pass that value to the mesh list, and return the appropriate mesh reference = ObjectList[ObjectYouWant].MeshNeeded MeshList[reference].GetMeshData; OR MeshList[ObjectList[ObjectYouWant].MeshNeeded].GetMeshData; Clunky, ineffecient. It would be a ton easier to just have a Mesh * (mesh pointer) in Object that points right at the darn Mesh Object (struct/element, what have you) You can actually do this, but when you try to reference it, it's out of scope and thus throws an error. &Object.meshPointer to reference a Mesh Object would be so much simpler & faster it seems, or is this strictly against Object Oriented design?

Share this post


Link to post
Share on other sites
I don't understand your problem. What you ask for is perfectly possible and not prevented in C++ at all. Just allocate objects on the heap if you need them after the function returns.

Perhaps you can show us more specifically what you have tried to do that doesn't work.

ETA: for example, the below code works just fine:


#include <iostream>
using namespace std;

class Foo
{
public:
int *id;
};

class Bar
{
public:
int data;
};

int main()
{
Foo foo;
Bar bar;

bar.data = 15;
foo.id = &bar.data;

cout << *foo.id;
bar.data = 73;
cout << *foo.id;
}


Output is 1573.

Share this post


Link to post
Share on other sites
As Hnefi says, what you're wishing for is possible. But if you're using a STL container, remember to be sure that your container type doesn't invalidate your pointer if the container needs to resize itself. (std::vector f.x.) I think a std::list would work, but you should check with your favorite STL documentation.

But as I see you use the word "List" in your own post, you might already be aware of this pitfall. [smile]

Share this post


Link to post
Share on other sites
Hrmmm, let me eplain in greater detail t hen.

Suppose you have two data lists: Information & Object. Each list Contains a nodes of information:

Information list ( InfoNode.next->InfoNode.next->....->next=Null )
Object list ( ObjectNode.next->ObjectNode.next->...->next=Null)

Say the ObjectNodes need to point to List Nodes, you can't because they're out of scope with one another.

ContainerClass{
InformationList list;
ObjectList list_two;
}

ObjectList[index].pointerToInfoNode = &InformationList[index];
is illegal because it's out of scope correct?

If i try to RunProcessOnObjectInformation(&ObjectList[index].pointerTo

[Edited by - echoredriot on May 26, 2008 8:34:06 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by echoredriot
Suppose you have two data lists: Information & Object. Each list Contains a nodes of information:

Information list ( InfoNode.next->InfoNode.next->....->next=Null )
Object list ( ObjectNode.next->ObjectNode.next->...>next=Null)

I'm with you so far.
Quote:
Say the ObjectNodes need to point to List Nodes, you can't because they're out of scope with one another.

I don't see how. You should be able to reference whatever you want just fine. As long as you didn't create an object on the stack in a statement block that has since gone out of scope, you should have no problems.
Quote:
ContainerClass{
InformationList list;
ObjectList list;
}

ObjectList[index].pointerToInfoNode = &InformationList[index];
is illegal because it's out of scope correct?

No, that has nothing to do with scope. It's illegal for entirely different reasons. ObjectList (and InformationList) is not an instance - it is a class. You can only access non-static members of instances. Also, you declared two instances with the same "list" in the same scope, which is illegal.

You need to read up a bit on classes, but for now, consider this analogy: your class "ObjectList" is to the variable "list" as the word "Car" is to the big piece of metal in your garage, which we can call "yourcar". If you need to see what colour "yourcar" is, you need to look at "yourcar" - you cannot get that information from "Car", because "Car" is just a description of cars in general.

Similarily, "ObjectList" describes a class. You cannot assign (non-static) variables in "ObjectList", because the information in the class describes ALL lists of that type.

To change your example into something correct would look something like this:

ContainerClass{
InformationList infoList;
ObjectList objList;
};

objList[index].pointerToInfoNode = &infoList[index];


Of course, you should not be able to use [index] with a list - that would be an array (or an extremely slow implementation of operator[]) - but that's a side issue.

Share this post


Link to post
Share on other sites

This topic is 3492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this