patterncppMinor
NamedPoint class using unique_ptr for members
Viewed 0 times
namedpointforusingmembersclassunique_ptr
Problem
After reading this old article from 2001 I have tried to implement the class from it using
An author's claim is that C++ is not appropriate for large software projects because
My primary complaint against C++ is that the language is so complicated, and has enough booby-traps, that average and above-average programmers have a difficult time writing code without serious bugs.
This is the test he gave to candidates on his job interviews:
As part of my standard interview for C++ candidates I ask them to write me a small class with the intention of evaluating their command of the language. This also gives us a reasonable coding sample to discuss during the interview. I can ask about potential improvements, extensions and testing strategies.
The request of the author is:
Write a Named Point class with three members: two floating point values for the coordinates on an X-Y plane, and a name represented as a 'char *'. Assume that this class will be used for some sort of wargame or simulation program that treats the world as flat and that these named points will be used to represent things like cities, battlefields, etc.
This is the version of class implementation from the article:
```
class NamedPoint
{
private:
float x;
float y;
char *name;
public:
NamedPoint (float x, float y, char *name)
{
this->x = x;
this->y = y;
if (name == NULL)
this->name = NULL;
else
{
this->name = new char[strlen(name) + 1];
strcpy (this->name, name);
}
}
~NamedPoint ()
{
if (name != NULL)
delete name;
}
// NOTE: Most interviewees start with a signature
// like this:
// NamedPoint (NamedPoint copy)
//
NamedPoint (const NamedPoint & copy)
{
this->x = copy.x;
this->y = copy.y;
if (copy.name != NULL)
{
this->name = new ch
unique_pointer. An author's claim is that C++ is not appropriate for large software projects because
My primary complaint against C++ is that the language is so complicated, and has enough booby-traps, that average and above-average programmers have a difficult time writing code without serious bugs.
This is the test he gave to candidates on his job interviews:
As part of my standard interview for C++ candidates I ask them to write me a small class with the intention of evaluating their command of the language. This also gives us a reasonable coding sample to discuss during the interview. I can ask about potential improvements, extensions and testing strategies.
The request of the author is:
Write a Named Point class with three members: two floating point values for the coordinates on an X-Y plane, and a name represented as a 'char *'. Assume that this class will be used for some sort of wargame or simulation program that treats the world as flat and that these named points will be used to represent things like cities, battlefields, etc.
This is the version of class implementation from the article:
```
class NamedPoint
{
private:
float x;
float y;
char *name;
public:
NamedPoint (float x, float y, char *name)
{
this->x = x;
this->y = y;
if (name == NULL)
this->name = NULL;
else
{
this->name = new char[strlen(name) + 1];
strcpy (this->name, name);
}
}
~NamedPoint ()
{
if (name != NULL)
delete name;
}
// NOTE: Most interviewees start with a signature
// like this:
// NamedPoint (NamedPoint copy)
//
NamedPoint (const NamedPoint & copy)
{
this->x = copy.x;
this->y = copy.y;
if (copy.name != NULL)
{
this->name = new ch
Solution
First of all, the class has very limited usability. Lookup by name will be much longer than lookup by id or index. Second,
Since the users need access to all members of the class, the members should be public. There is no real invariant to hold here. Using
class is not relevant here since it has setters and getters. I would just write this:#include
struct named_point
{
float x;
float y;
std::string name;
}Since the users need access to all members of the class, the members should be public. There is no real invariant to hold here. Using
std::unique_ptr for this job is somewhat odd. Use std::string. It will cleanup your code dramatically, since it implements rule of 5 itself, a lot of burden will be lifted.Code Snippets
#include <string>
struct named_point
{
float x;
float y;
std::string name;
}Context
StackExchange Code Review Q#135290, answer score: 2
Revisions (0)
No revisions yet.