🔒 Locked
Drawing function
Advertisement
Original Post
I have several basic shape classes that has a drawing function. This function should NEVER be called explicitly. I have a main Graphics class that when the user does Graphics.Render() all the Draw() functions are called in that. I have several variables in each of the shape objects that are private to the class so i would need to have a fucntion called Draw() and it would need to be public... but i don't want to have it able to be called from anywhere... any ideas?
tha's not working... i want private access to Line FROM graphics... i tried putting "friend class Line" in Graphics but that's not working..
here's what i have so far:
class Graphics
{
friend class Line;
MapList LineList;
Render();
// other crap
};
class Line
{
private:
Draw();
// other crap
};
What i have here is the ability to store pretty much an infinite amount of Lines to be rendered, in the render function, i want to be able to loop through each line and be able to do LineList->GetItem(i)->Draw(); with "i" being my increment
here's what i have so far:
class Graphics
{
friend class Line;
MapList
Render();
// other crap
};
class Line
{
private:
Draw();
// other crap
};
What i have here is the ability to store pretty much an infinite amount of Lines to be rendered, in the render function, i want to be able to loop through each line and be able to do LineList->GetItem(i)->Draw(); with "i" being my increment
You need friend class Graphics; in Line, not the other way around. If you have a Shape base class and you're inheriting that in all of your shape classes, then you should have friend class Graphics; in that and have a Draw() should be private virtual (actually that might be protected virtual - I can't quite remember how this works atm).
error C2253: 'Wrath2D::cWFigure::Draw' : pure specifier only applies to virtual function - specifier ignored
error C2248: 'Wrath2D::cWLine::Draw' : cannot access private member declared in class 'Wrath2D::cWLine'
i'm getting errors like that.
edit: oh yea, and i do have a base class called cWFigure, and i have bool Draw() = 0; in protected, is that correct?
error C2248: 'Wrath2D::cWLine::Draw' : cannot access private member declared in class 'Wrath2D::cWLine'
i'm getting errors like that.
edit: oh yea, and i do have a base class called cWFigure, and i have bool Draw() = 0; in protected, is that correct?
hmm... now that i think about it, i really just need the Draw() function to be a friend. so how would i do that?
ok i fixed the problem with the virtual function, now the only error i have is:
error C2248: 'Wrath2D::cWLine::Draw' : cannot access private member declared in class 'Wrath2D::cWLine'
and here's how things look:
what's wrong with that?
error C2248: 'Wrath2D::cWLine::Draw' : cannot access private member declared in class 'Wrath2D::cWLine'
and here's how things look:
class Graphics{private: List<Line> ListList;public: bool Render(); friend bool cWFigure::Draw();};class Figure{protected: virtual bool Draw() = 0;// bunch o' crap};class Line : public Figure{private: bool Draw();};what's wrong with that?
As far as I understand friends, which isn't far as I haven't had need to use them in any of my tinkering thus far... you use friend to give permission to another class to access member functions/variables of the class containing the friend statement.
So if, in your shape class, you: friend class graphics;, you will be granting access to the graphics class in that the graphics class will be able to call functions inside of shape objects even if they are protected/private (not sure which, although I do believe that it may be protected that doesn't share even with friends).
My two cents, something to chew on,
Vopisk
Edit: Once again, you have your friend declaration within the graphics class, it needs to be inside of the cWFigure class and should be: friend class graphics.
So if, in your shape class, you: friend class graphics;, you will be granting access to the graphics class in that the graphics class will be able to call functions inside of shape objects even if they are protected/private (not sure which, although I do believe that it may be protected that doesn't share even with friends).
My two cents, something to chew on,
Vopisk
Edit: Once again, you have your friend declaration within the graphics class, it needs to be inside of the cWFigure class and should be: friend class graphics.
hmmm... so since i'm only going to make a friend of that function, should i, in my Figure class, declare friend Graphics::Render()? that way the Render() has scope of everything inside figure?
i have found the solution... i have to declare "friend class Graphics;" in EVERY class that derives from Figure, not IN Figure; it works now
That is correct. When you name something a friend, it's easy enough to say "You are my friend", but someone else cannot walk up to you on the street and say "I am your friend!", because you know who your friends are and you've never seen this guy before. So when you declare another class a friend, you tell it all your secrets (you let it access your protected and private members). However, sometimes you don't want a whole class to know all of your dirty laundry, so you only want to reveal these things to a certain function within another class, you do this by declaring that function a friend.
So, you declare the function a friend within the class that you want to reveal the secrets of and thusly, that function will have access to all of the members of said class.
Hope this helps,
Vopisk
Edit: Oh yeah, I forgot to mention the fact that friends are not inherited... but you already figured that one out, but a good thing to let everyone else know if they're searching for the same answer. :)
So, you declare the function a friend within the class that you want to reveal the secrets of and thusly, that function will have access to all of the members of said class.
Hope this helps,
Vopisk
Edit: Oh yeah, I forgot to mention the fact that friends are not inherited... but you already figured that one out, but a good thing to let everyone else know if they're searching for the same answer. :)
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement