Skip to main content
gamedev.net
🔒 Locked

std::vector questions

Started by Programmer16 4 replies 600+ views
Original Post
Programmer16
Programmer16
My biggest question is how I would replace code like this with a vector:
class CObject
{
public:
    virtual HRESULT Render() = 0;
    CObject* m_pNext;
};
My other questions are: ->Is there anyway to access the data members of a vector class without setting a pointer:
vector<POINT> Point;
POINT AddPoint(int x, int y)
{
    POINT pt;
    pt.x = x;
    pt.y = y;
    return pt;
}

Point.push_back(AddPoint(3,5));

MoveTo(Point[0].x,Point[0].y);
Second would be how would I go about using push_back() without overriding a constructor or making a function like above. Thanks in advance! /* I use DirectX 8.1 and C++ (Microsoft Visual C++ 6.0 Professional edition) */
Fruny
Fruny

#include <vector>
#include <algorithm>
#include <functional>

class CObject
{
public:
virtual ~CObject() {} // Base class destructor must be virtual

virtual HRESULT Render() = 0;
};

vector<CObject*> objects; // Polymorphism requires using a container of pointers.

// Many caveats apply if you want to avoid memory leaks.


// Call the Render function on each object

std::for_each( objects.begin(), objects.end(),
std::mem_fun(&CObject::Render) );

//Or if you prefer

std::vector<CObject*> itor, end = objects.end();
for( itor = objects.begin(); itor != end; ++itor)
(*itor)->Render();


Is there anyway to access the data members of a vector class without setting a pointer

Question hazy, try again later.

Second would be how would I go about using push_back() without overriding a constructor or making a function like above.

You need to pass an already constructed object. No way around that.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
silvermace
silvermace
(i hope this isnt a joke thread. no offensive if it genuinly isn't)

A vector is meant for storage with random access, your CObject
class looks to me like a linked-list, you probably shouldnt
replace a link-list with a vector if you dont have to; a vector
is intended for random access, where rendering is usually a
consecutive access type task, so a linked-list is perfect for
this.

if you're really hell-bent on using the STL you could try this:

class CObject {
public:
void Render();
};

std::list< CObject > m_scene;

int ApplicationInit() {
//just add the objects you want rendered

m_scene.push_back( CObject() );
....
}

int RenderScene() {
std::list< CObject >::iterator i = m_scene.begin();
for(; i!=m_scene.end(); ++i)
(*i).Render();
}



your last two questions are far too vague/incoherant to answer
are you sure you understand the basic concepts behind the
std::vector class?

here are some links:
www.codeproject.com/vcpp/stl/stlintroduction.asp
www.cprogramming.com/tutorial/stl/vector.html
cplus.about.com/library/weekly/aa110103a.htm


[Irix]

[EDIT: formatting, grammar]


[edited by - silvermace on March 4, 2004 2:30:19 AM]
Amnesty
Amnesty

class CObject
{
public:
virtual HRESULT Render() = 0;
};

class ObjectVectorListThingy : public std::vector<CObject*>
{
~ObjectVectorListThingy()
{
for (std::vector<CObject*>::iterator i = begin(); i != end();i++)
{
delete (*i);
(*i) = NULL;
}
}
}; // not tested


edit - I diecieded to delete half of it.



[edited by - Amnesty on March 4, 2004 2:32:10 AM]
"I turned into a driveway I thought was mine and crashed into a tree I don't have."- a real insurance claim
mattnewport
mattnewport
quote:
A vector is meant for storage with random access, your CObject class looks to me like a linked-list, you probably shouldnt replace a link-list with a vector if you dont have to; a vector is intended for random access, where rendering is usually a consecutive access type task, so a linked-list is perfect for this.

I wouldn''t agree with that. In general a vector is a better choice than a linked list unless you''ve got a very specific reason for using a linked list (e.g. you need to insert and delete large objects often in the middle of the list). For a collection of pointers a vector is a much better choice than a list - a list carries a fair bit of overhead in both memory usage and access time over a vector for a collection of small items that doesn''t change too frequently but is frequently iterated over.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.