POV-Ray : Newsgroups : povray.off-topic : My first C++ program : Re: My first C++ program Server Time
30 Sep 2024 13:19:10 EDT (-0400)
  Re: My first C++ program  
From: Warp
Date: 19 Sep 2008 10:56:08
Message: <48d3bd88@news.povray.org>
Invisible <voi### [at] devnull> wrote:
> > bool StackCheck(const std::vector<int>& stack, int min)
> > 
> >   Usually with big objects (such as vectors) you basically always want
> > to pass by const reference rather than by value.

> Ooo... I didn't realise you could do that. (Pass a const reference, that 
> is.) So that means, what? If you accidentally try to change it, you'll 
> get a compiler warning?

  No, you get a compiler error. Since you declared the parameter as const,
you *can't* modify it.

  Declaring everything you don't intend to modify as 'const' is a good
programming practice in C++ for many reasons:

1) If you don't intend to modify the parameter, very especially if the
   parameter is a reference (in which case you would be modifying the
   original rather than a copy), then you will get a compiler error if
   you try to modify it anyways. This will catch programming mistakes
   at compile time, which is a good thing.

2) Declaring a reference parameter as 'const' is self-documenting: You are
   not only telling the compiler that that parameter is not modified in the
   function, more importantly you are telling that to a human reader. In
   other words, you are effectively documenting that the parameter is a
   pure input parameter, not something the function uses to pass values
   to the calling code.

3) As a bonus, it is possible that in certain situations the compiler might
   be able to optimize const variables better than non-const ones.

  Personally I have got the habit of declaring *everything* that I don't
intend to modify as 'const'. Even simple local variables. For example
I will typically write code like this:

    const int retVal = someFunction();
    somethingElse();
    return ratVal;

  In principle there's no need for the 'const' there, but I have got the
habit of doing it. After a while it becomes self-documenting even for
myself.

> Can you do that with non-reference types?

  Yes. Anything can be declared 'const', even variables and parameters
which are passed by value. In other words, it's perfectly possible to
do this:

void foo(const int value)
{
    const int anotherValue = value+1;
    ...
}

  In practice you seldom see 'const' used in function parameters like
that, though.

  Note that (not surprisingly) pointers are slightly more complicated
with regard to 'const':

    const Type* ptr = something;

  This defines a pointer named 'ptr' which points to a const value.
In other words, you can't modify the value pointed by 'ptr'. However,
you can modify 'ptr' itself, to make it point somewhere else (or make
it null, of whatever).

  If what you want is for 'ptr' itself to be const, not the value it's
pointing to, you have to write it like this:

    Type* const ptr = something;

  Now you can modify the value pointed by 'ptr' but you can't modify 'ptr'
itself. Naturally if you want *both* things, you write:

    const Type* const ptr = something;

  Now you can't modify either 'ptr' nor the value it's pointing to.

  References do not have this kind of complication because the reference
variable itself is always const. What the 'const' keyword determines is
whether the object it's pointing to is const or not.

> Since you're here... do you happen to know the C++ name for stderr?

  std::cerr << "An error message.\n";

  (Anything taking an std::ostream& can be given std::cout or std::cerr
as parameter. Or an output file stream. Or an output string stream.)

> I was thinking along the lines of "each line of input is either +, -, or 
> a number". But I don't know how to convert a string into a number. 
> Apparently the ">>" operator will do this if you hand it a number-type 
> variable, so there must be some library function somewhere that does the 
> conversion. I just don't know it's name yet.

  The problem with the >> operator is that you have to know in advance
what the type of the input will be. In this case you don't know it because,
as you say, the input can be an operator (+ or -) or a number.

  What you can do is this:

    std::string s;
    while(true)
    {
        std::cin >> s;
        if(!std::cin.good()) break;

        int value;
        std::istringstream iss(s);
        iss >> value;
        if(!iss.fail())
            std::cout << "The input was an integer: " << value << "\n";
        else
            std::cout << "The input was something else: " << s << "\n";
    }

> >> PS. Nano is a horrid, horrid text editor!
> > 
> >   Why don't you use something better?

> Because my virtual machine is slow enough already without starting X11, 
> and Nano is the only text-mode editor I have available (other than Vim 
> and Emacs anyway).

  Why don't you use an editor in your host OS?

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.