Thread: Some questions

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    Some questions

    Hallo,

    I have a few questions I have been wondering about:

    In the function "void foo(const int)", what does the const do? I get how it is usefull if the value is passed as a reference, but when its not i dont understand what the const do.

    Is there a difference between void foo(int *a) and void foo(int &a)?

    At university, they teach us to always make all class variables private. Is this really a good idea? I see that is is useful is some cases (say, addMoney() to change the balance a bank account class).

    The last question is from a slide for a class in not taking( so I did not get the anware, just the question)
    How can you make a function that turns 1 into 0 and 0 into 1? Without using if statements.

    Regards,
    Ole Kristian

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In the function "void foo(const int)", what does the const do? I get how it is usefull if the value is passed as a reference, but when its not i dont understand what the const do.
    It serves as a reminder to the implementer of foo() that the parameter is not to be modified. To the caller it does not matter.

    Is there a difference between void foo(int *a) and void foo(int &a)?
    Of course. For the former a is a pointer to an int, for the latter a is an int reference.

    At university, they teach us to always make all class variables private. Is this really a good idea? I see that is is useful is some cases (say, addMoney() to change the balance a bank account class).
    They probably also talked about encapsulation and data hiding. Look up your notes for these concepts. That said, in some cases we could have a POD struct consisting entirely of public member variables. This would serve purely as a way of grouping some data.

    How can you make a function that turns 1 into 0 and 0 into 1? Without using if statements.
    With mathematics
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by h3ro View Post
    In the function "void foo(const int)", what does the const do? I get how it is usefull if the value is passed as a reference, but when its not i dont understand what the const do.
    Const itself tells the compiler that the data in this variable will not be modified, so you can understand how useful it is when using references... with a pass-by-value, it's rarely useful at all. But you can apply const to the whole function as well, which might be useful.

    Is there a difference between void foo(int *a) and void foo(int &a)?
    Although very similar, the biggest difference between references and pointers (when it comes to arguments) is that the syntax differs a bit.

    At university, they teach us to always make all class variables private. Is this really a good idea? I see that is is useful is some cases (say, addMoney() to change the balance a bank account class).
    It's certainly a good idea to make most member data private, but sometimes it's not such a bad idea to make some public.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A const in a function parameter can also indicate to the compiler better if it can optimize the function when inlining - although it can probably figure that out with flow-analysis.

    The difference between a pointer and a reference is small when it comes to the actual implementation in the compiler, but it's syntactically different, and it has slightly different meaning in the code - but the effect is very similar.

    Encapsulating the data (making it private) is a good way to ensure that you can change the internal data storage without changing the code that calls a function. Say you store some data in an array, and just let the code access the array directly. Then you find that you have a BIG performance problem, where a customer is getting 15 minute waiting times on the application, which is no good. So you have to re-organize the data into a binary tree, the usage time is now down to 5 seconds, brilliant. Simple change of the internal data structure, except all the places where you have to change the code that accesses the array directly, in two dozen different source files. If you hide all the data from the client software, then you can store the data in any way you like - even on a different computer via a database mechanism for example.

    There is of course a compromise between hiding the data and the overhead that implies, and the efficiency of accessing the data directly in the application. It will be the judgment of the implementor to decide which is more important, and where you should use data hiding/ecnapsulation, and where you should expose the data to the client software. A typical example where data hiding is not necessary would be a coordinate vector class for 3D graphics - there are only a few different ways you'd ever want to do that, so you may just as well expose the "float x, y, z" to the client software, rather than have the overhead of getting a "getx, gety, getz" function members. Of course, if you then want to do SSE/3DNow! optimisations on the data-flow, you'd struggle, becasue you actually want a vector of x, another vector of y and a third vector of z values, so that it is easy to do 2 or 4 math operations of the same type at once.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Thank you to all three of you, and especially to matsp for the in depth answer.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Oh, and your last question is simple:
    Code:
    int func(int x)
    {
         return !x;
    }
    By the way, if you want to make sure a value is either 0 or 1 as a truth value, then
    Code:
    int func(int x)
    {
       return !!x;
    }
    This will guarantee that the truth-value is the same (so passing in 4 will return 1, whilst passing in 0 will return 0).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM