473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

'x = this' in constructor

Is this safe?

-----------------
struct Foo;

struct Bar
{
Foo* f;
};

struct Foo
{
Bar b;
Foo()
{
// Stuff
b.f = this; // Is that safe?
// Stuff
}
};
Thanks.

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


Dec 4 '05 #1
7 1541

Alex Vinokur wrote:
Is this safe?

-----------------
struct Foo;

struct Bar
{
Foo* f;
};

struct Foo
{
Bar b;
Foo()
{
// Stuff
b.f = this; // Is that safe?
// Stuff
}
};


So long as Bar.f is a pointer I think you are ok. It's a cyclic
dependency though and best avoided if possible...sometimes it isn't.

http://c2.com/cgi/wiki?AcyclicDependenciesPrinciple

Dec 4 '05 #2
It depends on how you use it.. But i don't see anything wrong with
this. Just be careful that you don't destroy things more than once,
which might lead to undefined behaviour..

For instance, this would not be wise:
Foo* FooObj = new Foo;
//... use FooObj
delete FooObj->b.f; // Becouse f points to the same object that FooObj
points to, that object gets destroyed here..
//... The object that FooObj points to is no longer in existance here,
so..
FooObj->b.f = FooObj;
// That might look like restoring the address of the object that FooObj
*WAS* pointing to in b.f..
// But in fact, it is an error situation, becouse the object that
FooObj was pointing to, doesn't
// exist anymore and on most implementations (if not all), this would
couse undefined behaviour or couse an error.
delete FooObj; // This also is an error, becouse you're trying to
destroy an object that was already down the drain..

Dec 4 '05 #3
ro**********@gmail.com wrote:
dependency though and best avoided if possible...sometimes it isn't.

http://c2.com/cgi/wiki?AcyclicDependenciesPrinciple


So what? That page is just someone's ill-informed, inexperienced
opinion.

Just because someone dropped something into a Wiki doesn't mean it has
become gospel.

Dec 5 '05 #4
Kaz Kylheku wrote:
ro**********@gmail.com wrote:
dependency though and best avoided if possible...sometimes it isn't.
http://c2.com/cgi/wiki?AcyclicDependenciesPrinciple

So what? That page is just someone's ill-informed, inexperienced
opinion.

Just because someone dropped something into a Wiki doesn't mean it has
become gospel.


But this one is a "Principle"! It SAYS so! And it's based on what
someone else wrote, and even includes a link! So it must be true!

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Dec 6 '05 #5

Pete Becker wrote:
Kaz Kylheku wrote:
ro**********@gmail.com wrote:
dependency though and best avoided if possible...sometimes it isn't.
http://c2.com/cgi/wiki?AcyclicDependenciesPrinciple

So what? That page is just someone's ill-informed, inexperienced
opinion.

Just because someone dropped something into a Wiki doesn't mean it has
become gospel.


But this one is a "Principle"! It SAYS so! And it's based on what
someone else wrote, and even includes a link! So it must be true!


Joke around all you want guys but these things are well worth knowing
or at least familiarizing yourself with. As anyone who has actually
read these articles and principles knows, it is stated in several
places that some of these principles are actually contradictory.
Everything needs balance.

Cyclic dependencies can become a major hassle. The principle I cited
above applies to packages mainly but the reasoning in it can be applied
to classes also to a lesser degree.

Dec 6 '05 #6
ro**********@gmail.com wrote:

Cyclic dependencies can become a major hassle. The principle I cited
above applies to packages mainly but the reasoning in it can be applied
to classes also to a lesser degree.


Since packages aren't part of C++, it's rather difficult to assess that
statement. And since the link goes to a page with a rather rambling
discussion that's mostly lacking in actual analysis, it's not
particularly helpful.

There's nothing wrong with cyclic dependencies among classes when that's
what the problem domain requires.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Dec 6 '05 #7

Pete Becker wrote:
ro**********@gmail.com wrote:

Cyclic dependencies can become a major hassle. The principle I cited
above applies to packages mainly but the reasoning in it can be applied
to classes also to a lesser degree.


Since packages aren't part of C++, it's rather difficult to assess that
statement. And since the link goes to a page with a rather rambling
discussion that's mostly lacking in actual analysis, it's not
particularly helpful.


Obviously you guys just want to get into a flame war.

Packages are not defined by the standard but in practical terms they
are definately part of the C++ programmer experience.

Most people understand that cyclic dependencies are best avoided (yes,
in classes too) if practical so I'll just leave it at that now.

Dec 6 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Vinodh Kumar | last post by:
class A { int myIntVal; }; int main() { A a; return 0; }
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
4
by: Kench | last post by:
Sorry if this becomes a repost. I posted this to comp.lang.c++.moderated 1 hour ago still it does not show up there so posting this here. Hi, Consider class A & B both of which implement a copy...
3
by: ccs | last post by:
In Meyers' book he gave an example of "virtual copy constructor", which is quite different to an "ordinary" copy constructor by: 1. it returns a pointer to an object instead of a reference. 2. it...
17
by: highli | last post by:
When a non-default constructor provided in a class, the default constructor is not available anymore. In what cases shall a default constructor be defined explicitly? Specifically, in the...
2
by: vakap | last post by:
function show() { var s = '' ; for (var i = 0; i<arguments.length; s += '\n'+arguments) ; typeof(window) != 'undefined' ? window.alert(s) : WScript.Echo(s) ; } function f(){}...
7
by: ad | last post by:
When a class (say class1) inherited form another class (Class0). The constructor in the class0 will be executing before the constructor of class1. Can we over the constructor?
4
by: TJS | last post by:
How can I override the default values, of a class constructor , from an ASP.net page ? Public Sub New( ) HeaderStyle.BackColor = ColorTranslator.FromHtml("#5C85AD") HeaderStyle.ForeColor =...
8
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
4
by: Rahul | last post by:
Hi Everyone, It is well known that the input parameter which is passed to the copy constructor is passed as reference and not as as object. Because passing an object is as good as making another...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.