Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 05-14-2005, 10:40 AM   #1 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
TIP (5 minute read): remainder on const objects

Recap On const Methods

A const method is to be declared if this method is not allowed to modify the pointer this.

Example:
Code:
class AClass
{
public:
  //Must implement a constructor otherwise no const(ant) instance is allowed.
  AClass() {}
  void DoStuffNonConst()
  {
  }
  void DoStuffConst() const
  {
  }
};
DoStuffConst() won't be allowed to modify any members.
So far the recap.

Enforcing const(ness) of objects

One may want to create and intialize a const object:
Code:
const AClass theConstObject;
To enforce the constness of this object, this object may only call const methods:
Code:
class AClass
{
public:
  //Must implement a constructor otherwise no const(ant) instance is allowed.
  //Remember, const objects *must* be intialized. Hence the constructor.
  AClass() {}
  void DoStuffNonConst()
  {
  }
  void DoStuffConst() const
  {
  }
};

int main()
{
  const AClass theConstObject;
  theConstObject.DoStuffNonConst(); // ILLEGAL.
  theConstObject.DoStuffConst(); // LEGAL.
 
  return 0;
}
__________________
Valmont is offline   Reply With Quote
Old 05-18-2005, 08:22 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Well since you all had time to get used to this, here is a little homework assignment:\

Using the class in the first thread add pointer of type double as the internal representation.
1) Now define a member "double* DoStuffConst() const" in such a way, so this pointer can be modified after all. You may not use casts.
2) What is the lesson you learned?
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
confused on const object or const member function deepak_kr_mehta Standard C, C++ 2 05-11-2005 07:09 AM
about const object and const member function deepak_kr_mehta Standard C, C++ 1 05-06-2005 05:01 AM
Tip (5 minute solution): The Stopwatch Valmont Standard C, C++ 0 03-18-2005 08:27 AM
Tip (5 minute solution): The Facade Pattern Valmont Standard C, C++ 3 02-25-2005 12:27 AM


All times are GMT -8. The time now is 10:06 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting