CS253

CS253: Software Development with C++

Spring 2017

Duck Typing

See this page as a slide show

Duck Typing

CS253 Duck Typing

Rules for Rulers

Rules for Containers

Enforcement

Example

A vector requires that its stored type be copyable, both via copy ctor and assignment operator. For example, you can have a vector<string>, because strings are copyable

string now = "20241122T024848";
vector<string> v = {now, now, now};
cout << v.size() << '\n';
3

Example

How about a non-copyable class?

class Mom {
  public:
    Mom() = default;                        // I like the default ctor.
    Mom(const Mom &) = delete;              // You can’t change Mom!
    Mom &operator=(const Mom &) = delete;   // You can’t change Mom!
};

int main() {
    Mom helen;
    vector<Mom> v = {helen, helen, helen};
}
c.cc:10: error: use of deleted function 'Mom::Mom(const Mom&)'

How enforcement “works”

Modified: 2017-05-09T21:25

User: Guest

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building