Skip to content

Instantly share code, notes, and snippets.

View kalman5's full-sized avatar

Gaetano kalman5

View GitHub Profile
@kalman5
kalman5 / noncopyable
Last active August 29, 2015 13:56
Disable copy constructor and assignment operator
class NonCopyable {
public:
NonCopyable(const NonCopyable&) = delete;
NonCopyable& operator=(const NonCopyable&) = delete;
private:
...
}
@kalman5
kalman5 / doubleprinter
Created February 16, 2014 09:24
DoublePrinter
class DoublePrinter {
public:
void print(double aDouble) {
std::cout << "Value is: " << aDouble << std::endl;
}
};
DoublePrinter dp;
@kalman5
kalman5 / doubleprinterdelete
Last active August 29, 2015 13:56
DoublePrinterDeleteConversion
class DoublePrinter {
public:
void print(double aDouble) {
std::cout << "Value is: " << aDouble << std::endl;
}
void print(float) = delete;
};
@kalman5
kalman5 / crashdestination
Last active August 29, 2015 13:56
Possible reference to temporary
class CrashDestination {
public:
CrashDestination(const std::string& aString)
: theString(aString)
{}
void print() const { std::cout << theString << std::endl; }
private:
const std::string& theString;
};
@kalman5
kalman5 / rvaluedisabled
Last active August 29, 2015 13:56
rvalue reference disabled
class CrashDestination {
public:
CrashDestination(const std::string& aString)
: theString(aString)
{}
CrashDestination(const std::string&&) = delete;
void print() const { std::cout << theString << std::endl; }
private:
const std::string& theString;
@kalman5
kalman5 / gist:9451810
Last active August 29, 2015 13:57
const qualifier
class T {
...
foo() const; // Here *this is const
...
}
@kalman5
kalman5 / gist:9455164
Created March 9, 2014 21:42
ref-qualifier
class T {
...
foo() const; // *this is const
bar() &; // *this is an l-value
goo() &&; // *this is an r-value
...
};
@kalman5
kalman5 / gist:9455286
Created March 9, 2014 21:51
Factory Getting big object by copy.
class JumboFactory {
...
Jumbo getJumboByCopy() {
return theJumboObject;
}
...
private:
Jumbo theJumboObject;
};
@kalman5
kalman5 / gist:9455314
Created March 9, 2014 21:53
Useless copy
Jumbo myJumbo = JumboFactory().getJumboByCopy();
@kalman5
kalman5 / gist:9455397
Created March 9, 2014 21:58
Avoid the Copy
class JumboFactory {
...
Jumbo getJumboByCopy() const & {
//Deep copy
return theJumboObject;
}
Jumbo getJumboByCopy() && { // *this is an r-value
//Move
return std::move(theJumboObject);
}