arnuld wrote:
Earl Purple wrote:
Any law against giving your struct a trivial constructor?
i am a newbie to C++ so i dont know what are constructors.
I know that Stroustrup doesn't believe in teaching C first so I don't
get why he hasn't yet taught using constructors.
Anyway, in its simplest form, a constructor is a simple function. Now
maybe he has taught you that in C++ structs and classes are the same
and that they can have member functions that access their member
variables. The constructor is the first function that is called when
you create an instance of a struct or class - it is only called at this
time and cannot be subsequently called later. It has the form that I
have shown you and in its simplest form is used to initialise the class
members.
Bad use of pointers for more than one reason. The user is never going
to pass in a NULL one (if they do it's UB).
what does this mean?
It means "undefined behaviour", which is what happens in C++ when you
do something illegal. It will often cause your program to crash but is
not guaranteed to do so, and it may work at first then crash or err
later on.
In addition your print
function isn't going to modify the date so use a const reference. There
is another big reason why you'll want your function to take a const
reference as you'll see below. Anyway
that is a good reason & was my intention. i tried it before but it raises an error.
Probably because you did it wrong. Did you use -syntax? With a
reference you use . syntax.
Of course we can do better than specify cout as the output source. The
best is simply to overload operator << for dates so
std::ostream & operator<<( std::ostream & os, const Date & vdate )
{
os << vdate.month << '\n' << vdate.date << '\n' << vdate.year <<
'\n';
return os;
}
out of my head.
ostream is a generic stream. The above writes the data of your class in
text format into any stream but it doesn't specify what type of stream
it is. It could be to the console, it might be to a file or it might be
to memory.
void print_all_dates( const std::vector< Date & vdates )
{
std::for_each( vdates.begin(), vdates.end(), print_date );
}
good. i will do this.
This is the simplest form of an algorithm .It is clear this is not
above your head so perhaps you can start to think along these lines.
When you want print_date to take extra parameters then you can learn to
write a simple functor.
If you want to use operator<< then it's a bit more complex but
something you should get used to:
void print_all_dates( const std::vector< Date & vdates )
{
std::copy
(
vdates.begin(), vdates.end(),
std::ostream_iterator< Date >( cout, "" ) // "" is a
delimiter
);
}
out of my head.
Yes, probably would be if std::ostream is, as we've taken this to a
higher level of complexity.
std::copy is a generic algorithm that takes a sequence and writes it to
another location. You've come across iterators: they use operator* and
operator ++ just like pointers do, except that they are more generic
than a pointer, i.e. a pointer is a special type of iterator.
Algorithms make use of the fact that iterators use operator++ and
operator*. operator* normally returns a reference to something. Now
references can be const (read-only) or non-const. copy requires a
non-const reference because it is going to write to it using
assignment (operator=).
However it doesn't have to be a reference at all. It can be an instance
of a class that wraps a reference. It can even be the iterator itself.
In fact ostream_iterator is a cheat with regards to operator++ as it
doesn't change anything at all - it always writes to the end of the
stream. It is also a cheat with regards to operator* which also returns
itself. All the work is done in operator= which simply writes the
object to the stream.
Note that back_inserter is another example of an iterator that
"cheats".
that is fine & i will use them in future when i will be able to
understand them & that will happen when Stroustrup will explain.
I'm sure he will. If not, then read Scott Meyers' "Effective STL". It
was that book that converted me to using them.