interface principle

B

bluekite2000

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?
 
V

Victor Bazarov

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
 
B

bluekite2000

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.
 
V

Victor Bazarov

[...]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top