class vs struct mindset

The Partridge Family were neither partridges nor a family. Discuss.
albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: class vs struct mindset

Post by albinopapa » October 19th, 2017, 8:16 pm

Actually, according the guidelines, you would in fact use class with public access to create an interface...huh.

Interface class
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: class vs struct mindset

Post by albinopapa » October 19th, 2017, 8:23 pm

Lol, then go and fuck it up in the next section:

Code: Select all

struct Device {
    virtual void write(span<const char> outbuf) = 0;
    virtual void read(span<char> inbuf) = 0;
};

class D1 : public Device {
    // ... data ...

    void write(span<const char> outbuf) override;
    void read(span<char> inbuf) override;
};

class D2 : public Device {
    // ... different data ...

    void write(span<const char> outbuf) override;
    void read(span<char> inbuf) override;
};
Could just be another oversight I suppose.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: class vs struct mindset

Post by albinopapa » October 19th, 2017, 8:28 pm

Hey, found a guideline that I can agree with

Virtual functions should specify exactly one of virtual, override or final

Basically, base class should be the only one to have the virtual in front of the functions while derived classes should either have override or final appended to the function signature.

Putting virtual on derived functions to me made it seem like there were going to be derived classes from the derived class ( grandchildren ). I never liked it, it confused me. I like specifying override letting me know it overrides a virtual function and haven't used final yet, but I guess I should start.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: class vs struct mindset

Post by albinopapa » October 19th, 2017, 8:51 pm

Man, one could spend hours correcting the guideline document.

The PImpl idiom

Code: Select all

class widget {
    class impl;
    std::unique_ptr<impl> pimpl;
public:
    void draw(); // public API that will be forwarded to the implementation
    widget(int); // defined in the implementation file
    ~widget();   // defined in the implementation file, where impl is a complete type
    widget(widget&&) = default;
    widget(const widget&) = delete;
    widget& operator=(widget&&); // defined in the implementation file
    widget& operator=(const widget&) = delete;
};
Here the private data is listed first before the public interface.

This seems a lot like how people feel about the bible with all sorts of contradictions.

Another thing I find weird is unique_ptr not complaining about using a forward declared class.
I get the message:
Error C2027 use of undefined type 'Stub::Impl' testbed2 f:\program files (x86)\microsoft visual studio 14.0\vc\include\memory 1193

Code: Select all

stub.h
class Stub
{
public:
	Stub(std::string Msg);

	void PrintMsg();
private:
	class Impl;
	std::unique_ptr<Impl> pImpl;
};

stub.cpp
class Stub::Impl
{
public:
	Impl( std::string Msg );
	void PrintMsg()const;
private:
	std::string msg;
};

Stub::Stub( std::string Msg )
	:
	pImpl( std::make_unique<Impl>( std::move( Msg ) ) )
{}

void Stub::PrintMsg()
{
	pImpl->PrintMsg();
}

Stub::Impl::Impl( std::string Msg )
	:
	msg( std::move( Msg ) )
{}

void Stub::Impl::PrintMsg() const
{
	std::cout << msg << std::endl;
}
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply