Home arrow C and C+ arrow Misc C++ Tips
Misc C++ Tips PDF
Wednesday, 25 March 2009
This section shows you different C++ programing tips.


Tip1. Properly define your functions (methods) within your classes
----------------------------------------------------------------------------


If you have the following code:


#include <iostream>

using namespace std;

class address
  {
    public:
      void show_address(void) { cout << street << ' ' << number << endl; };
      address() {  number = 0; street = new char [500]; };
      address(char* street, int number)
        {
          address::street = new (nothrow) char [500];
          strcpy(address::street, street);
          address::number = number;
        };
      ~address() { delete street; }

    private:
      char* street;
      int number;
  };

int main(void)
  {
    address addrs("Main Street", 10);
    addrs.show_address();
  }

And when you try to compile you get the following warning:

test1.cc: In function 'int main()':
test1.cc:25: warning: deprecated conversion from string constant to 'char*'


Then it means the compiler warns you that your function is allowed to modify memory, and it should not.

So instead of       address(char* street, int number)     you should have:       address(const char* street, int number)


 
< Prev   Next >

Other BSD Systems

OpenBSD

Misc

Solaris

Polls

Best BSD firewall?