0

I am a student,Learning C++ Concurrency Programming.I want to know how to access Functionality both Graphic Card chip and Sound Card chip through my C++ program.Is there another way to connect respective drivers and my application without using external libraries such as OpenGL,OpenAL etc.Can you briefly describe the solution by an example. Thank you.

Buddhika Chaturanga
  • 877
  • 2
  • 16
  • 28
  • I gone through this web site: http://duramecho.com/ComputerInformation/WhyHowCppConst.html but could not grasp the exact idea I wanted.Need better explanation how to put those "const" keyword appropriately on each place and how it affects to program efficiency.? – Buddhika Chaturanga Aug 26 '13 at 11:17
  • 4
    I have a simple rule for my self: When there's something that I don't need to change, then I use `const`. – Some programmer dude Aug 26 '13 at 11:22
  • Perhaps you should say what *you* think the word means, and then what it means in each usage above. You did, after all, just run through a tutorial. Did you learn *anything* from it? – WhozCraig Aug 26 '13 at 11:23
  • Please have a look at http://stackoverflow.com/questions/5598703/c-const-usage-explanation: it should give you a pretty good idea of `const` usage – gu1d0 Aug 26 '13 at 11:25
  • 1
    @JoachimPileborg: My rule is to make everything const, unless whatever I want to do with it needs something else. – PlasmaHH Aug 26 '13 at 11:29
  • Rule of thumb 'as const as possible' – Sambuca Aug 26 '13 at 11:31

2 Answers2

11
const int * const function_name(const int * point) const;
//(1)       (2)                 (3)                (4)

The first prevents the returned pointer from being used to modify whatever data is being accessed by the function. This is good (unless you want to modify it), since it prevents unexpected modification of shared data.

The second prevents the caller from modifying the returned pointer. This has no effect when returning a built-in type (like a pointer), and can be bad when returning a class type since it inhibits move semantics.

The third prevents the function from modifying the data. This is good (when appropriate) for the same reason as the first.

The fourth prevents the function from modifying the (non-mutable) state of the object. This is good (when appropriate) as it allows the function to be called on const objects, and makes it easier to keep track of when the object might change.

const int * const Functiontwo(const int * const &) const;
//                                        (5)

The only new thing here is passing the pointer by const reference. There's no point in doing that; passing by value gives the same semantics, and is less verbose and probably more efficient. Passing by const reference can be more efficient than passing by value for types that are large or otherwise expensive to copy.

A slight variation is to pass a const value:

const int * const Functiontwo(const int * const) const;
//                                        (6)

This is of no interest to the caller (indeed, the function can be declared with or without it without changing the meaning); so there's no point putting it on a declaration, just on the definition. It means that the pointer can't change within the function. This can be good (as can declaring local variables const), as it can make the function implementation easier to follow.

Mike Seymour
  • 242,813
  • 27
  • 432
  • 630
  • wrt the last example: since this is actually a function *declaration*, the top level const on the parameter has no sense at all. The function *definition* can leave or add top-level const as it sees fit. – Arne Mertz Aug 26 '13 at 11:44
  • @ArneMertz: Indeed; I've updated to say more explicitly that there's no point putting a top-level `const` in a declaration. – Mike Seymour Aug 26 '13 at 12:10
  • @gx_: Good spot, I didn't properly read what I pasted. – Mike Seymour Aug 26 '13 at 12:22
  • @All How do we use (const int * const &) within our function without an identifier ? I think it should (const int * const & x) or something? – Buddhika Chaturanga Aug 26 '13 at 16:16
  • @BuddhikaChaturanga: That's right (assuming we want to use the parameter in the function). The declaration could look like this, but the definition would have to name the parameter in order to use it. – Mike Seymour Aug 26 '13 at 16:17
1

const after a member function means that the function does not modify the state of the parent object:

struct foo
{
    int bar() const; // does not modify the instance of foo
    int & bar(); // might modify the instance of foo
};

In your example your functions return

int const * const

The first const means, that the int you are pointing to must not be changed; you tell your user that he may read the value, but not modify it. This is also relevant for const-correctness:

int const * foo() const; // can be used from const functions
int * foo(); // cannot be used from const function

The second const means that the pointer itself is const, meaning that:

int * ptr = &a;
*ptr = 42; // valid
ptr = &b; // valid

int const * ptr = &a;
*ptr = 42; // INVALID
ptr = &b; // valid

int * ptr const = &a;
*ptr = 42; // valid
ptr = &b; // INVALID

int const * ptr const = &a;
*ptr = 42; // INVALID
ptr = &b; // INVALID

Making the pointer itself const as return value from a function is hardly ever useful.

For references, a quick guide for C++03 is that you want to pass by reference if you want to modify the object, and want the modification to stick; you pass by value if you want to modify the object but do not want the modification to stick (a copy is made), or (exception!) if the value you are passing is a POD type; you pass by constant reference if you do not need to modify a non-POD type and thus save yourself the copy costs:

void foo(int & a); // changes to a are applied to a itself, no copy made
void foo(int a); // changes to a are applied to a copy of a, copy made
void foo(std::string const & a); // no changes allowed, no copy made
nijansen
  • 8,409
  • 9
  • 48
  • 69