[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppGossip
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppGossip
(C++) Gossip
Classes I often use to determine what happens 'behind the scenes'. I have two versions: a simple one and a version you can let your class inherit from Gossip.Simple Gossip class
struct Gossip { Gossip() { std::cout << "Constructor" << std::endl; } ~Gossip() { std::cout << "Destructor" << std::endl; } Gossip(const Gossip& g) { std::cout << "Copy constructor" << std::endl; } Gossip& Gossip::operator= (const Gossip& g) { if (this == &g) { std::cout << "Assigment operator: prevented self-assignment" << std::endl; return *this; } std::cout << "Assigment operator" << std::endl; return *this; } //Prefix Gossip& operator++() { std::cout << "Gossip prefix increment operator" << std::endl; ++mX; //Increment return *this; //Return class reference } //Postfix Gossip operator++(int) { std::cout << "Gossip postfix increment operator" << std::endl; Gossip old(*this); //Copy ++(*this); //Increment original using prefix return old; //Return old copy } private: int mX; };
- include <iostream>
Heritable base class
class Gossip { public: Gossip() { std::cout << "Contructor" << std::endl; } Gossip(const Gossip& gossip) { std::cout << "Copy contructor" << std::endl; } ~Gossip() { std::cout << "Destructor" << std::endl; } Gossip Gossip::operator=(const Gossip& other) { std::cout << "Assignment operator" << std::endl; return *this; } void gossipPublic() const { std::cout << "Gossip public" << std::endl; } protected: void gossipProtected() const { std::cout << "Gossip protected" << std::endl; } private: void gossipPrivate() const { std::cout << "Gossip private" << std::endl; } }; class GossipDerived : public Gossip { public: GossipDerived() { std::cout << "Derived contructor" << std::endl; } GossipDerived(const GossipDerived& gossip) { std::cout << "Derived copy contructor" << std::endl; } ~GossipDerived() { std::cout << "Derived destructor" << std::endl; } GossipDerived GossipDerived::operator=(const GossipDerived& other) { std::cout << "Assignment operator" << std::endl; return *this; } void gossipDerivedPublic() const { std::cout << "GossipDerived public" << std::endl; } protected: void gossipDerivedProtected() const { std::cout << "GossipDerived protected" << std::endl; } private: void gossipDerivedPrivate() const { std::cout << "GossipDerived private" << std::endl; } };
- include <iostream>
Topic links
Code links
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
