Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to return a structure? 1

Status
Not open for further replies.

Warrioruw

Programmer
Jan 30, 2004
24
0
0
CA
Hi all,

I want to return a structure in a function, but compiler complains that I don't have copy constructor for the structure. Does anyone know how to create a copy constructor for a structure?

Thanks.

 
Code:
struct Foo
{
  // Copy ctor
  Foo(const Foo& src):mSomeMember(src.mSomeMember)
  {
  }

  int mSomeMember;   
};

/Per
[sub]
"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."[/sub]
 
Doesn't microsoft's compiler provide a default copy contructor?
 
Usually yes.

/Per
[sub]
"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."[/sub]
 
But if the structure in question contains pointers, then the default bitwise copy is almost always the wrong thing to do.

Normally, you need to duplicate the things being pointed to as well.

--
 
Perf, you wrote:

Code:
struct Foo
{
  // Copy ctor
  Foo(const Foo& src):mSomeMember(src.mSomeMember)
  {
  }

  int mSomeMember;   
};

Whats that syntax of Foo mean?

ie the:
Foo (const Foo& src):mSomeMember(src.mSomeMember)

??

Whats the colon and the src.mSomeMember mean?

Cheers

Skute

"There are 10 types of people in this World, those that understand binary, and those that don't!"
 
Well, given that its a copy constructor - can't you guess what it means? It initializes its members by using another Foo as input.

>Whats the colon
The part following the : is the initialization list where the Foo can initialize its members and call its base class' constructor (not in this example though) even before the code between the { } starts executing.

>and the src.mSomeMember mean?
Well, since src (short for source) is a reference to a Foo, src.mSomeMember is the mSomeMember member of the Foo refered to by src.

Some links on the subject:


/Per
[sub]
"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."[/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top