- Best way to implement class printing - 7 Updates
- Rick C. Hodgin - 8 Updates
- troubles overloading << in a templated class - 4 Updates
- vector dealocation in a destructor - 6 Updates
JiiPee <no@notvalid.com>: Nov 07 01:13AM They say, especially in game programming, that drawing/printing should not be done inside the class but some other class should do it (by asking the data from the object). Is this the way also with cout? Say we have: class Person { public: Person(const string& name, int age, int id) : m_name{ name }, m_age{ age }, m_id{ id } {} private: string m_name; int m_age; int m_id; }; If I want to create a print function which prints all persons details what is the best way to do it? First idea/try is obviously somethin like: class Person { public: Person(const string& name, int age, int id) : m_name{ name }, m_age{ age }, m_id{ id } {} void print(ostream& os) { os<<m_name<<", " << ....}; private: string m_name; int m_age; int m_id; }; But now the class is doing two things... printing and holding the data. they should be separated, isnt it? Second idea is to create a non-class function which a friend of Person. but any other ideas? Is it best to do like in game programming: no printing done in Person class, some other function/class asks the values and prints. Obviously we dont want to have getters for Person necessary (because it would break OO-idea.). |
bitrex <bitrex@de.lete.earthlink.net>: Nov 06 08:51PM -0500 On 11/06/2016 08:13 PM, JiiPee wrote: > They say, especially in game programming, that drawing/printing should > not be done inside the class but some other class should do it (by > asking the data from the object). I thought the notion of OOP was exactly the opposite: the caller tells the object in question to do something, not asks for the data and does the thing itself. |
Daniel <danielaparker@gmail.com>: Nov 06 06:12PM -0800 On Sunday, November 6, 2016 at 8:51:54 PM UTC-5, bitrex wrote: > I thought the notion of OOP was exactly the opposite: the caller tells > the object in question to do something, not asks for the data and does > the thing itself. Strict OO people believed that, yes. But I thought that'd all died off. The first problem with objects that display themselves, print themselves, etc. is that the object doesn't know how the user wants to present it: an object should be viewable in multiple ways, as the user wishes. The second problem is that it leads to very heavy objects, and single responsibility principle demurs. In general, the code for displaying, printing, etc should be kept outside the object. The object should expose enough of itself to make that possible. Daniel |
"Öö Tiib" <ootiib@hot.ee>: Nov 06 09:59PM -0800 On Monday, 7 November 2016 03:13:42 UTC+2, JiiPee wrote: > They say, especially in game programming, that drawing/printing should > not be done inside the class but some other class should do it (by > asking the data from the object). Is this the way also with cout? Maybe. It is up to you. You are designer of your software. > }; > If I want to create a print function which prints all persons details > what is the best way to do it? There are no best ways. I can suggest to consider two. Both are Ok but you may want to use something else. 1) A free 'operator<<' is likely least noise: std::ostream& operator<<(std::ostream& o, Person const& p); 2) Visitor pattern is most fancy and clever. The Wikipedia https://en.wikipedia.org/wiki/Visitor_pattern has a C++ example how different "files" are "dispatched" by "dispatchers" using visitor pattern. You can use same to "output" your "outputtables" with "outputters". Then you can have number of different and separate ways how your 'Person' and other types are printed, drawn or stored on different media or views. My experience is that visitor pattern works greatly but is bordering on confusing for some people. |
Jerry Stuckle <jstucklex@attglobal.net>: Nov 07 09:45AM -0500 On 11/6/2016 8:51 PM, bitrex wrote: > I thought the notion of OOP was exactly the opposite: the caller tells > the object in question to do something, not asks for the data and does > the thing itself. Yes, to a point. But the display can also be a class, as in Microsoft's Document-View architecture, or the Model-View-Controller (MVC) architecture common to many projects. The class holding the data is still responsible for some formatting of the data. And for a text-mode console or flat file mode I/O, that is often sufficient. But when you have a GUI there is much more involved and more interaction between the various objects. In cases like this, it is often more efficient (and cleaner) to have a class representing the view (or multiple classes for multiple views). Like anything else, there are few firm design rules. You need to become familiar with different architectures so you can adapt to the needs. -- ================== Remove the "x" from my email address Jerry Stuckle jstucklex@attglobal.net ================== |
JiiPee <no@notvalid.com>: Nov 07 06:25PM On 07/11/2016 02:12, Daniel wrote: > Strict OO people believed that, yes. But I thought that'd all died off. > The first problem with objects that display themselves, print themselves, etc. > is that the object doesn't know how the user wants to present it: yes , exactly. And also... the format of printing can be changed easier outside. Maybe today we want to pring on to screen but tomorrow into the file etc. maybe next day using OpenGL.... > demurs. In general, the code for displaying, printing, etc should be kept > outside the object. The object should expose enough of itself to make that > possible. you propose: string getName(); ? so getters for those data memember? |
JiiPee <no@notvalid.com>: Nov 07 06:29PM On 07/11/2016 05:59, Öö Tiib wrote: > My experience is that visitor pattern works greatly but is bordering > on confusing for some people. good suggestion. I ll think about this. Although it makes things a bit complicated, isnt it? a simple textout functionality needs a complex stucture behind it... |
"Rick C. Hodgin" <rick.c.hodgin@gmail.com>: Nov 06 05:59PM -0800 Chris M. Thomasson wrote: > IIRC, there is a place in the Bible where God says that there are > more stars than grains of sand on the Earth. God's thoughts toward each of us individually outnumber the sand: http://biblehub.com/kjv/psalms/139.htm 17 How precious also are thy thoughts unto me, O God! how great is the sum of them! 18 If I should count them, they are more in number than the sand: when I awake, I am still with thee. The count of stars and sand are often used interchangeably in the Bible. Man has determined there are more stars than grains of sand though astronomy and max volume count computations. Best regards, Rick C. Hodgin |
"Chris M. Thomasson" <invalid@invalid.invalid>: Nov 06 06:38PM -0800 On 11/6/2016 5:59 PM, Rick C. Hodgin wrote: > The count of stars and sand are often used interchangeably in the > Bible. Man has determined there are more stars than grains of sand > though astronomy and max volume count computations. Well, there are a lot of stars in our own galaxy. Then think of all the galaxies! Trust me, we are not alone. ;^) |
"Chris M. Thomasson" <invalid@invalid.invalid>: Nov 06 06:50PM -0800 On 11/6/2016 5:59 PM, Rick C. Hodgin wrote: > The count of stars and sand are often used interchangeably in the > Bible. Man has determined there are more stars than grains of sand > though astronomy and max volume count computations. Seriously, I want to know. Do you think that Earth is the only planet with sentient beings? |
"Rick C. Hodgin" <rick.c.hodgin@gmail.com>: Nov 06 07:31PM -0800 On Sunday, November 6, 2016 at 9:50:40 PM UTC-5, Chris M. Thomasson wrote: > > though astronomy and max volume count computations. > Seriously, I want to know. Do you think that Earth is the only planet > with sentient beings? I trust in what God has told us. The Bible teaches us the path to salvation, the path to forgiveness of sin by His Son, Jesus Christ. The Bible teaches us that there are things we know today only in part, but that then on the other side of death we will then know in full. The Bible has not stated there are other beings in the universe, only that there are other beings in the Third Heaven (an invisible realm which someone Paul knew was taken there and where he heard things that aren't even lawful for a man to speak about): http://biblehub.com/kjv/2_corinthians/12.htm 2 I knew a man in Christ above fourteen years ago, (whether in the body, I cannot tell; or whether out of the body, I cannot tell: God knoweth;) such an one caught up to the third heaven. 3 And I knew such a man, (whether in the body, or out of the body, I cannot tell: God knoweth;) 4 How that he was caught up into paradise, and heard unspeakable words, which it is not lawful for a man to utter. 5 Of such an one will I glory: yet of myself I will not glory, but in mine infirmities. 6 For though I would desire to glory, I shall not be a fool; for I will say the truth: but now I forbear, lest any man should think of me above that which he seeth me to be, or that he heareth of me. There is clearly other life in the universe. Is it other life like us? Since the Bible records it was unlawful for a man to speak about, and that he heard those things, they were not spoken by man. They were of some other kind of beings, which I will reckon are members of the citizens of Heaven, eternal beings that are of a form and manner of varying consistency as we see here on Earth with the varying form and manner of people groups. God is diverse. His creativity and resourcefulness in Creation is diverse. What He has created here on Earth is astounding, and yet everything here is today under sin and fallen in disgrace and wearing down. Yet in eternity, things are spiritual, not natural, and there is no wearing down. Jesus Christ is life, and the wellspring of it. Everything He's created is of life in this universe. It is only because of sin here upon this Earth that things here die, and I believe that is also part of His design, for God would've known when creating Satan how he would turn, and how Adam and Even would've fallen, and He already had a plan formed before He laid the foundation of the world to rescue us, to save all those who would be saved, and to eternally damn all those who would not be saved. I believe that in the pattern God gave Abraham, the one to whom He made his promise to bless all nations that bless him, and curse all nations that curse him, that on the 8th day they should be circumcised as part of an everlasting covenant with God, that He has done the same thing in Creation. Six days of Creation. One day of rest. And then the 8th day begins, the eternal day, whereby the excess flesh is shed, and the ongoing covenant with God is established. The shedding of that foreskin flesh is a precursor of the shedding of the excess flesh from this world, and of those who will not be saved, such that those who are saved enter into that ongoing covenant, with the eighth day being an eternal day. ----- God has given us many patterns here on Earth for what's coming in eternity. Marriage. Seasons. Building blocks. Even the New Jerusalem coming down out of Heaven is in the shape of a massive 1500 mile cube. It is clear this is part of a much larger work God is creating. Our goals here in this world are to come to His Son and be saved, and to then go out and do labor to reach others, so that as the seed was planted in us of our faith in Him, so it will grow and produce much fruit, so that by all of our corporate work (Christians, and ultimately also the Jews who are saved), that at harvest time the bounty will be full and rich, so that the next phase of our existence will be before us, and and we will not be cast into the fire, will not be lost. A myriad of life awaits us. At the very least there will be things we'll be able to hear and know then which are illegal for us to speak about here on Earth today, but I believe this is only a small fraction of what's really going on up there because Jesus said that in His Father's House there are many mansions, and that He's going up there to prepare a place for us. As the pattern was given to man here on the Earth that we were to subdue it, and there was land here a plenty for all to labor and work and share and grow, so is the bounty of Heaven. How big is the Earth compared to a man? And how big is the universe compared to this Earth? And what lies beyond this universe that cannot be seen? And how big is it compared to this universe? God has plenty in store for all of us. Our goals here upon this Earth are to salvation, and then teaching others unto salvation, so that we are saved, married to Jesus Christ, and then we enter into that eternal eighth day after the seven days of Creation are fulfilled here upon this Earth. Best regards, Rick C. Hodgin |
"Chris M. Thomasson" <invalid@invalid.invalid>: Nov 07 12:48AM -0800 On 11/6/2016 7:31 PM, Rick C. Hodgin wrote: > The Bible teaches us that there are things we know today only in part, > but that then on the other side of death we will then know in full. > The Bible has not stated there are other beings in the universe, only [...] So, I take it your answer to my simple question is no: You think we are all alone, simply because the Bible does not mention it. I wonder what you would do if aliens just happened to present themselves. Would you totally freak out? ;^) |
"Rick C. Hodgin" <rick.c.hodgin@gmail.com>: Nov 07 03:28AM -0800 Chris M. Thomasson wrote: > I wonder what you would do if aliens just happened to > present themselves. Would you totally freak out? I would conclude they are what they are: demon liars masquerading as aliens to deceive people so they'll believe the lie, remain unforgiven, and enter into Hell for all eternity upon their death. I believe "aliens" will be used as the cover story when the rapture takes place. There are already preparations for it. A NASA feed was cut off unexpectedly after an object entered the field of view. The Pope has said he would welcome aliens as brothers and sisters in Christ. The Vatican has a large telescope called "Lucifer" (not kidding) in their basement, and a team searching for extra- terrestrial life. I believe it's schooling unaware to prepare nonbelievers for the deception that's coming over the whole Earth, so they will relate to and embrace the lie. Best regards, Rick C. Hodgin |
leigh.v.johnston@googlemail.com: Nov 07 04:30AM -0800 On Monday, November 7, 2016 at 11:28:24 AM UTC, Rick C. Hodgin wrote: > and embrace the lie. > Best regards, > Rick C. Hodgin And that ladies and gentlemen is what is known in the trade as "batshit crazy" |
"Rick C. Hodgin" <rick.c.hodgin@gmail.com>: Nov 07 05:28AM -0800 > > deception that's coming over the whole Earth, so they will relate to > > and embrace the lie. > And that ladies and gentlemen is what is known in the trade as ".. crazy" The rapture is by no means a certainty. There are passages which support it, and passages which support it does not exist. It depends on how you look at things. Other things are more clear, however. The Pope, for example, has just released six new Beatitudes. The Beatitudes are those things Jesus spoke on the Sermon on the Mount in Matthew 5-7. The Pope did not introduce some new guidance for Catholics, or to issue a manmade doctrine to augment our existence as by a church activity. He called them Beatitudes, placing himself on the same level with Christ, stating that what Jesus gave us was insufficient, and that he then has the power, authority, and right to introduce new teachings. It's one of the most sinister things anyone's ever done with regards to an attack against Jesus, because this one comes in the form of a friendly addition designed to help out believers, and our fellow man. ----- There are real things taking place in this world which will affect us in eternity. I am advising every person to seek out the truth for themselves so they will come to a full knowledge of the truth, and to teach you that such a pursuit of the truth will lead you to Jesus Christ, to forgiveness, to salvation, and to a new nature you've never imagined possible -- the born again nature. Best regards, Rick C. Hodgin |
Popping mad <rainbow@colition.gov>: Nov 07 06:52AM I have a class that has a std::vetcor of a type of a templated class that looks like this template<class cost> class Pstates { public: Pstates (std::vector< state<cost> > x) : _vstate{x} { for( const auto& y : _vstate) std::cout << y << std::endl; }; etc I can get this to print. the overload looks like this friend std::ostream &std::operator<<( std::ostream &output, const tree::state<cost> &p ) { output << "nam => " << p.nam() << "\tcost=> " << p.r() << std::endl; return output; } The error that I'm getting is: || g++ -Wall -ggdb -c nodes.cpp || g++ -Wall -ggdb -o fitch.o -c fitch.cpp || nodes.h: In instantiation of 'tree::Pstates<cost>::Pstates (std::vector<tree::state<cost> >) [with cost = int]': fitch.cpp|15 col 47| required from here nodes.h|158 col 16| error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'const tree::state<int>') || std::cout << y << std::endl; || ~~~~~~~~~~^~~~ /usr/include/c++/6.2.1/ostream|628 col 5| note: candidate: std::basic_ostream<_CharT, _Traits>& std::operator<< (std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = tree::state<int>] <near match> || operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x) I thought that const tree::state<int> was a match http://www.nylxs.com/docs/thesis/src/fitch/ and http://www.nylxs.com/docs/thesis/src/fitch/nodes.h are the full source. It is small yet. |
"Öö Tiib" <ootiib@hot.ee>: Nov 06 11:25PM -0800 On Monday, 7 November 2016 08:52:46 UTC+2, Popping mad wrote: > the overload looks like this > friend std::ostream &std::operator<<( std::ostream &output, const > tree::state<cost> &p ) You lost me right here. Why operator is qualified as 'std::operator<<'? Do you want it to be in namespace 'std'? Why that operator is friend of 'tree::Pstates<cost>'? A name first declared in a friend declaration within class or class template X becomes a member of the innermost enclosing namespace of X [IOW 'tree'], but is not accessible for lookup (except argument-dependent lookup that considers X [IOW 'tree::Pstates<cost>' that is not among its arguments]) unless a matching declaration at the namespace scope is provided [that is not] - see namespaces for details. I put mine comments in []. So lookup can't find that operator of yours. |
Popping mad <rainbow@colition.gov>: Nov 07 11:21AM On Sun, 06 Nov 2016 23:25:17 -0800, Öö Tiib wrote: > You lost me right here. > Why operator is qualified as 'std::operator<<'? Do you want it to be in > namespace 'std'? Why that operator is friend of 'tree::Pstates<cost>'? It is qualified as std because we are operating in the namespace of tree http://stackoverflow.com/questions/236801/should-operator-be-implemented- as-a-friend-or-as-a-member-function#237074 > template X becomes a member of the innermost enclosing namespace of X > [IOW 'tree'], but is not accessible for lookup (except > argument-dependent lookup that considers X [IOW 'tree::Pstates<cost>' I don't know what this means. What is IOW? > that is not among its arguments]) unless a matching declaration at the > namespace scope is provided [that is not] - see namespaces for details. > I put mine comments in []. So lookup can't find that operator of yours. What? |
Paavo Helde <myfirstname@osa.pri.ee>: Nov 07 02:40PM +0200 On 7.11.2016 13:21, Popping mad wrote: >> [IOW 'tree'], but is not accessible for lookup (except >> argument-dependent lookup that considers X [IOW 'tree::Pstates<cost>' > I don't know what this means. What is IOW? http://lmgtfy.com/?iie=1&q=what+is+IOW%3F |
Popping mad <rainbow@colition.gov>: Nov 07 02:04AM Since we can't just delete a vector, do I need to deallocate it when we call a destructor? I think I do, but I don't know how. |
"Öö Tiib" <ootiib@hot.ee>: Nov 06 09:09PM -0800 On Monday, 7 November 2016 04:04:42 UTC+2, Popping mad wrote: > Since we can't just delete a vector, do I need to deallocate it when we > call a destructor? I think I do, but I don't know how. It feels that I misunderstand what you are asking. We call a destructor of object manually on very rare circumstances. Destructor is called automatically when the lifetime of an object ends. So manually we call it on case when we want to hack with these life-times with placement news and such. |
"Christopher J. Pisz" <cpisz@austin.rr.com>: Nov 06 11:34PM -0600 On 11/6/2016 8:04 PM, Popping mad wrote: > Since we can't just delete a vector, do I need to deallocate it when we > call a destructor? I think I do, but I don't know how. I am not sure what you are referring to when you say "deallocate it" It is best to not use any pronouns at all, imo, when talking programmin. Like every class, std::vector has its own deconstructor that is called when it goes out of scope. The deconstructor will clean up any memory the vector used internally. However, if the objects the vector contains, used any memory themselves, then those objects are responsible for cleaning up that memory. If you allocated things yourself and put those things into the vector, then you are going to be responsible for releasing them as well. |
ruben safir <ruben@mrbrklyn.com>: Nov 07 01:15AM -0500 On 11/07/2016 12:34 AM, Christopher J. Pisz wrote: thanks |
Popping mad <rainbow@colition.gov>: Nov 07 06:21AM On Sun, 06 Nov 2016 21:09:15 -0800, Öö Tiib wrote: > It feels that I misunderstand what you are asking. Well, I am building the destructor for a class which has large vectors and so I was thinking I needed to manually delete them within the destructor. But if I understand correctly, vectors will trigger their own destructor when they reach the end of their life. |
"Öö Tiib" <ootiib@hot.ee>: Nov 06 10:58PM -0800 On Monday, 7 November 2016 08:21:52 UTC+2, Popping mad wrote: > and so I was thinking I needed to manually delete them within the > destructor. But if I understand correctly, vectors will trigger their > own destructor when they reach the end of their life. Yes. Most C++ programmers follow RAII idiom and everything in standard library supports it so if we did not allocate memory manually (like with 'new', 'malloc' or [yuck] 'strdup') then we don't need to manually deallocate it. Note that if you did actually allocate your vector with 'new' because you have raw pointer to vector member then you have one level of indirection too many and should refactor it to just vector data member to get rid of that 'new'. |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment