Jump to content
  • Advertisement
Sign in to follow this  
andyb716

c++ object template

This topic is 5062 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

Hi, I have a object factory that deals with rendering objects. I have simple objects like triangles, lines, and quads, meshes etc, in a vector for each of them. What I would like to do is just to have one vector of a base object type and depending on what the object is call the render method for each object. So my render loop would look like this. But I'm new to templates all I can do is have a function where you can pass any number, double float, int etc and it would handle it so I would have to overload the method. But I have no idea how it works I just copied some code. If there are some websites where I can learn more please let me know, or if you can help me with this specific problem it would be greatly appreciated thanks.

std::for_each(objects.begin(), objects.end(), renderObject);

void renderObject(Object *object)
{
 object->Render();
}


Share this post


Link to post
Share on other sites
Advertisement
Well, your code could be replaced by:

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

which removes a step. Note that you might need std::mem_fun_ref if you're using a container of Objects, as opposed to a container of Object pointers.

For your exact question : I'm a little confused? Is each object responsible for rendering itself? If so - can't you use inheritance:


class Object
{
public:
virtual void Render() = 0;
};

class Line : public Object
{
public:
virtual void Render() {//draw line}
};

class Triangle : public Object
{
public:
virtual void Render() {//draw triangle}
};




Am I missing something?
Jim.

Edited : stupid mistakes

Share this post


Link to post
Share on other sites
Yah I am using inheritance but I don't want to have 5 for_each statements
like, sorry but I really don't understand this too well, thanks for the help
for_each(quads.begin(), quads.end(), renderQuads());

renderQuads(Quad quad)
{
quad.render();
}

Share this post


Link to post
Share on other sites
If you're using inheritance then you can store a derived class in a base-class pointer. Consequently you can store a list of different derived classes, thusly:



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

class Object
{
public:
virtual void Render() = 0;
};

class Line : public Object
{
virtual void Render() {std::cout << "Drawing Line";}
};

class Triangle : public Object
{
virtual void Render() {std::cout << "Drawing Triangle";}
};


int main()
{
// Create an empty vector of smart pointers
std::vector<Object*> myObjects;

myObjects.push_back(new Line);
myObjects.push_back(new Triangle);

std::for_each(myObjects.begin(), myObjects.end(), std::mem_fun(&Object::Render));

std::cin.get();

delete myObjects[0];
delete myObjects[1];

}



Am I still missing the point?
Jim.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!