468,110 Members | 1,573 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,110 developers. It's quick & easy.

interface principle

I have
//vector_header.h
template <typename T>
class Vector
{
....
}

//vector_func.h
#include vector_header.h
template<typename T>
Vector<T> sqrt(Vector<T>& Vin);
#include "vector_func.cc"

//vector_func.cc
template<typename T>
Vector<T> sqrt(Vector<T>& Vin)
{
....
}

My question is: Is sqrt() logically a part of class Vector? If it s
not, should I make it so? How do I go about making it a part of class
Vector? I d rather not put it in the same header file. Can I put it in
the same namespace instead?

Aug 12 '05 #1
3 1353
bl**********@gmail.com wrote:
I have
//vector_header.h
template <typename T>
class Vector
{
...
}

//vector_func.h
#include vector_header.h
template<typename T>
Vector<T> sqrt(Vector<T>& Vin);
I thought it should be

... sqrt(Vector<T> const & Vin);

(notice the 'const')
#include "vector_func.cc"

//vector_func.cc
template<typename T>
Vector<T> sqrt(Vector<T>& Vin)
{
...
}

My question is: Is sqrt() logically a part of class Vector?
No.
If it s
not, should I make it so?
Depends. If you accept my 'const' suggestion, then any other object
that can be converted to 'Vector<??>' will be accepted as the argument
of 'sqrt'. If you make it a member, then such conversion will not be
applicable.
How do I go about making it a part of class
Vector?
Uh... How do you make it a part? You mean, a member, right? What
else is "a part"?
I d rather not put it in the same header file. Can I put it in
the same namespace instead?


I don't understand this question. If it's a member, then it's gotta
go in the same header. If it's not a member, you can put it anywhere
you like.

V
Aug 12 '05 #2
No I dont mean a member. According to Sutter's Interface Principle
1. For a class X, all functions, including free functions, that both
"mention X" and are "supplied with" X (ie. the function and X are in
the same namespace/header file) are logically part of X, because they
form part of the interface of X.
2. Therefore, both member and nonmember functions can be logically
"part of" a class. A member function function is still more strongly
related to a class than is a nonmember, however.

Since my sqrt() is in another header file, is it still "part of" class
Vector? And does it matter anyway and this interface principle stuff??
My program works fine as it is.

Aug 12 '05 #3
bl**********@gmail.com wrote:
[...]
Since my sqrt() is in another header file, is it still "part of" class
Vector?
If you consider it interface, it's interface. If the user of your class
will need to include other headers to complete the declarations of the
interface, so what?
And does it matter anyway and this interface principle stuff??
My program works fine as it is.


It probably doesn't matter.

Now I am puzzled a little bit. If you are quoting Sutter's interface
principle, you must agree with it, no? If you agree with it, what made
you ask the question you asked? I mean, do you still have doubts about
your 'sqrt' template's being part of 'Vector's interface? What causes
those doubts?

V
Aug 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by The Directive | last post: by
26 posts views Thread by Marius Horak | last post: by
47 posts views Thread by Matthew | last post: by
3 posts views Thread by Martin Arvidsson \(Visual Systems AB\) | last post: by
reply views Thread by YellowFin Announcements | last post: by
2 posts views Thread by Diego F. | last post: by
52 posts views Thread by Ben Voigt [C++ MVP] | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.