You must know something, for else you would not be asking. How does your problem manifest itself?
Btw, a compiler does warn about three statements in your code. The second is most likely a logical error:
In constructor 'first::first()':
17:12: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
In destructor 'first::~first()':
55:18: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
In function 'int main()':
65:30: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
I just was in chapter 11(virtual functions)in(object oriented programing -robert lafrofe-) and I wanted to build my own program about copy constructor and (= overloading) but here it is the original program from the book and I just don't have any idea why mine is not working
because I am just abeginner and my programme take about 5 hours.
// strimem.cpp
// memory-saving String class
// overloaded assignment and copy constructor
#include <iostream>
#include <cstring> //for strcpy(), etc.
using namespace std;
////////////////////////////////////////////////////////////////
class strCount //keep track of number
{ //of unique strings
private:
int count; //number of instances
char* str; //pointer to string
friend class String; //make ourselves available
//member functions are private
//--------------------------------------------------------------
strCount(char* s) //one-arg constructor
{
int length = strlen(s); //length of string argument
str = new char[length+1]; //get memory for string
strcpy(str, s); //copy argument to it
count=1; //start count at 1
}
//--------------------------------------------------------------
~strCount() //destructor
{ delete[] str; } //delete the string
};
////////////////////////////////////////////////////////////////
class String //String class
{
private:
strCount* psc; //pointer to strCount
public:
String() //no-arg constructor
{ psc = new strCount("NULL"); }
//--------------------------------------------------------------
String(char* s) //1-arg constructor
{ psc = new strCount(s); }
//--------------------------------------------------------------
String(String& S) //copy constructor
{
psc = S.psc;
(psc->count)++;
}
//--------------------------------------------------------------
~String() //destructor
{
if(psc->count==1) //if we are its last user,
delete psc; // delete our strCount
else // otherwise,
(psc->count)--; // decrement its count
}
//--------------------------------------------------------------
void display() //display the String
{
cout << psc->str; //print string
cout << " (addr=" << psc << ")"; //print address
}
//--------------------------------------------------------------
void operator = (String& S) //assign the string
{
if(psc->count==1) //if we are its last user,
delete psc; // delete our strCount
else // otherwise,
(psc->count)--; // decrement its count
psc = S.psc; //use argument's strCount
(psc->count)++; //increment its count
}
};
////////////////////////////////////////////////////////////////
int main()
{
String s3 = "When the fox preaches, look to your geese.";
cout << "\ns3="; s3.display(); //display s3
String s1; //define String
s1 = s3; //assign it another String
cout << "\ns1="; s1.display(); //display it
String s2(s3); //initialize with String
cout << "\ns2="; s2.display(); //display it
cout << endl;
return 0;
}
Also an object with a count of zero doesn't make sense, when nothing is using that object
This. In other words, recheck the overall logic of your program.
it just didn't work
When you say "does not work" and do not explain, does it allow us to answer "you code has an error" without further explanation?
Explaining the error in detail is an essential part of finding the solution. In order to explain thoroughly, you end up thinking every step in your code and probably learn to spot the actual problem too.
The indentation of your code is not very informative (IMHO). It is hard to see individual (member) functions and local scopes among lines of text.
Copy constructor creates/initializes an object, just like the other constructors. What do you delete there?
I just didn't know that the copy constructor was the first constructor
I did believe that the non argument constructor take place first
but I built a normal function it's only purpose to use the copy constructor and nothing happened
1 2
void test(first q,first w)//////////////
{}
then I add bool variable and the non argument constructor and the one argument constructor
set it to false
and the copy constructor now can see if the object had met any constructor before him
and every thing now is working properly
For primitive types (pointers, ints, bool, etc), they are not initialized -- they contain whatever arbitrary junk happened to be at that memory location previously.
A: We do not know the value of bar.
One more time:
1 2 3 4 5 6 7 8 9 10 11 12
class Foo {
bool bar;
T * ptr;
public:
Foo( const Foo & ) {
if ( bar ) { // ERROR: bar has not been set yet, so could be either true or false
if ( 42 == ptr->gaz ) { // ERROR, the ptr does not point to anything valid yet
delete ptr; // ERROR, the ptr does not point to anything valiid yet
}
}
}
};