Re: class vs struct mindset
Posted: 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
Interface class
Learn to program or die trying
https://forum.planetchili.net:443/
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;
};
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;
};
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;
}