Sponsored Content
Top Forums Programming Doubts on C++ copy constructor concept Post 302503573 by royalibrahim on Friday 11th of March 2011 01:48:55 AM
Old 03-11-2011
Doubts on C++ copy constructor concept

Hi,

If I run the following program

Code:
class A {
public:
    A()  { cout << "default" << endl; }
    A(const A&) { cout << "copy" << endl; }
};

A tmp; 
A fun() {
    return tmp;
}

A test() {
    A tmp;
    cout << &tmp << endl;
    return tmp;
}

int main() {
    A a = fun();
    A b = test();
    cout << &b << endl;
}

I am getting the following output:

default
copy
default
0xbfbe1792
0xbfbe1792

My doubts are:

1) Why 'const' keyword is so necessary (without which compiler is throwing error) here? why it is not optional and why compiler is stressing to add it which is not happening in normal times?

2) Copy constructor is called when an object is returned by value from a function, as it is one of the place where copy constructor should be called. But, why it is not happening during the second object b's creation?

3) In the above code the output shows the same address on the stack for the 2 objects, but the same code in C using structures instead of classes shows a different address. I know local variables (and objects) inside a function are created in the stack and getting destroyed after the function call. Hence, theoretically it should point to different addresses, with a local copy for each one. But, why in C++, it is holding the same address??
 

7 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Constructor problem

Hi guys I am new to these forums but since I am taking a class at college I would appreciate any help that is possible for this program. My instructor said that when its complete the program should be able to store all 3 fields instead of just 1. public class Greeter2Test { public static... (4 Replies)
Discussion started by: woot4moo
4 Replies

2. Programming

why constructor cannot be virtual

helo i read many books but i cant find the proper answer that why constructor cannot be virtual can u explain me in simple term that why constructor cannot be virtual Regards, Amit (2 Replies)
Discussion started by: amitpansuria
2 Replies

3. Programming

what is diff b/w copy constructor and overloaded assignment operator

Helo i m new in c++. i m confuse about what is exact difference b/w copy constructor and overloaded assignment operator. Regards, Amit (3 Replies)
Discussion started by: amitpansuria
3 Replies

4. Programming

how do you handle a constructor and destructor that fail

helo i m new in c++ on linux can u tell me with an simple example that how do you handle constructor and destructor that fail? Regards, Amit (4 Replies)
Discussion started by: amitpansuria
4 Replies

5. Programming

Doubt regarding Copy Constructor and return value

Hi All, I have made the simple following program :- #include <string> #include <iostream> using namespace std; class A{ private: int val; public : A(){cout<<"In A()"<<endl;} A (const A& aa) { cout<<"In copy c'tor"<<endl; } }; A f(... (1 Reply)
Discussion started by: shubhranshu
1 Replies

6. Programming

c++ object constructor question

I have the following code class Param{ public: Param(int aa, int bb){ a=aa; b=bb; } int a,b; }; void function(Param); int main(){ function(2,3); return 0; } (6 Replies)
Discussion started by: santiagorf
6 Replies

7. Programming

Constructor?

I am learning about C++ and today am reading concepts for Constructor but it seems a bit difficult to grab it fully. Please anyone explain in simple words about Constructor? (1 Reply)
Discussion started by: ggiwebsinfo
1 Replies
FBB::OFoldStream(3bobcat)                                         Fold long lines                                        FBB::OFoldStream(3bobcat)

NAME
FBB::OFoldStream - Folds long lines SYNOPSIS
#include <bobcat/ofoldstream> Linking option: -lbobcat DESCRIPTION
FBB::OFoldStream folds long lines written to it. The OFoldStream writes the (folded) lines to a second ostream which is either used by or opened by the OFoldStream object. OFoldStream objects never fold lines in the middle of series of non-blank characters but will always break a line at white space charac- ters. The resulting lines will always appear to the right of a configurable left margin and to the left of a configurable right margin. There is a somewhat pathological exception to this: if a word is too long to fit in between the margins then the word will exceed the right hand margin. The indentation used for the left margins is configurable to either blanks (the default) or tabs. When tabs are used the width of a tab character is configurable, using a default of 8 positions in the destination stream. OFoldStream is implemented as a wrapper class around std::ostream and FBB::OFoldStreambuf and a more complete description of the folding process can be found in the ofoldstreambuf(3bobcat) man page. NAMESPACE
FBB All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace FBB. INHERITS FROM
std::ostream, (and privately from FBB::OFoldStreambuf) ENUMERATION
The enumeration TabsOrBlanks is used to select tabs or blanks when writing the indentation. The default is blanks. When tabs are selected the display width of tabs characters can be configured as well (using the default of 8 positions for each tab-character. The enumeration has two values: o BLANKS: The default, indicating that the left margin is specified and written as a number of blanks; o TABS: Indicating that the left margin is specified and written as a number of tab-characters. The enumeration TrailingBlanks is used to configure the OFoldStream object with respect to any trailing blanks that may appear on the final line. It is the same enumeration type as used with OFoldStreambuf (cf. ofoldstreambuf(3bobcat)) having two values: o IGNORE_TRAILING_BLANKS: This indicates that trailing blanks appearing at the final line if it is not terminated by a newline should not be written to the destination std::ostream. This is the default used by OFoldStream objects. o KEEP_TRAILING_BLANKS: This indicates that trailing blanks at the final line if it is not terminated by a newline should be written to the destination std::ostream CONSTRUCTORS
o OFoldStream(): This constructor initializes an OFoldStream object but does not associate it with a destination stream. It uses the values 0, 80, BLANKS, and IGNORE_TRAILING_BLANKSfor, resp. its left margin, right margin left-margin characters and TrailingBlanks handling mode. o OFoldStream(char const *fname, size_t leftIndent = 0, size_t rightMargin = 80, TabsOrBlanks tob = BLANKS, TrailingBlanks tb = IGNORE_TRAILING_BLANKS): This constructor initializes an OFoldStream object and opens (using std::ios::out) the destination stream using the name specified as its fname argument. o OFoldStream(std::ostream &stream, size_t leftIndent = 0, size_t rightMargin = 80, TabsOrBlanks tob = BLANKS, TrailingBlanks tb = IGNORE_TRAILING_BLANKS): This constructor initializes an OFoldStream object and uses as its destination stream the std::ostream stream. The destructor writes any buffered information to the destination stream and will then flush the destination stream. MEMBER FUNCTIONS
All members of std::ostream are available, as FBB::OFoldStream inherits from this class. o void close(): This member flushes any pending information to the destination stream and then closes the destination stream. o void open(char const *fname, openmode mode = std::ios::out): This member associates the OFilterStream object with an std::ofstream object whose filename is provided and that should receive the folded information. o void open(std::ostream &out): This member associates the OFilterStream object with the provided ostream object. o void setMargins(size_t leftMargin, size_t rightMargin): This member can be used to modify the left- and right folding margins. Note that the left margin may also be modified using the FBB::lm and FBB::mlm manipulators. o void setTrailingBlanks(TrailingBlanks tb): This member can be used to modify the currently used TrailingBlanks parameter. o void useBlanks(): This member can be used to select blanks to be used when inserting left margins. o void useTabs(size_t tabWidth = 8): This member can be used to select tab-characters to be used when inserting left margins. The second parameter is used to specify the display width of a tab-character. STATIC MEMBER FUNCTIONS
o size_t leftMargin(std::ostream const &os): This member returns the current left margin setting of the OFoldStream object passed to it as its argument. The member defines a std::ostream parameter since in many cases the OFoldStream object will be used in functions themselves defining std::ostream parame- ters. Internally, the std::ostream's std::streambuf is down cast to an OFoldStreambuf and an FBB::Errno exception is thrown if that cast fails. o size_t rightMargin(std::ostream const &os): This member returns the current right margin setting of the OFoldStream object passed to it as its argument. The member's parameter is down cast in the same way as leftMargin()'s argument: an FBB::Errno exception is thrown if that cast fails. EXAMPLE
#include <iostream> #include <string> #include <bobcat/ofoldstream> using namespace std; using namespace FBB; void margins(ostream &out) { cout << OFoldStream::leftMargin(out) << ", " << OFoldStream::rightMargin(out) << endl; } int main() { OFoldStream out(cout, 4, 40); out << lm(4); string line; while (getline(cin, line)) out << line << ' '; margins(out); return 0; } FILES
bobcat/ofoldstream - defines the class interface SEE ALSO
bobcat(7), lm(3bobcat), mlm(3bobcat), ofoldstreambuf(3bobcat) BUGS
None Reported. DISTRIBUTION FILES
o bobcat_3.01.00-x.dsc: detached signature; o bobcat_3.01.00-x.tar.gz: source archive; o bobcat_3.01.00-x_i386.changes: change log; o libbobcat1_3.01.00-x_*.deb: debian package holding the libraries; o libbobcat1-dev_3.01.00-x_*.deb: debian package holding the libraries, headers and manual pages; o http://sourceforge.net/projects/bobcat: public archive location; BOBCAT
Bobcat is an acronym of `Brokken's Own Base Classes And Templates'. COPYRIGHT
This is free software, distributed under the terms of the GNU General Public License (GPL). AUTHOR
Frank B. Brokken (f.b.brokken@rug.nl). libbobcat1-dev_3.01.00-x.tar.gz 2005-2012 FBB::OFoldStream(3bobcat)
All times are GMT -4. The time now is 08:51 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy