C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Passing Pointers
Goto page Previous  1, 2, 3 ... 16, 17, 18, 19  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
E. Robert Tisdale
Guest





PostPosted: Sat Apr 21, 2007 6:01 am    Post subject: Re: Copy constructor question Reply with quote



James Kanze wrote:
Quote:
Victor Bazarov wrote:

And there is probably no need for the copy c-tor as it's implemented.
The one the compiler will generate is absolutely sufficient.

Still, most coding guidelines would require an explicit one, to
avoid making it inline.

Victor is correct of course.
Actually, I would write

//A(const A& a): myvalue(a.myvalue) { }// copy

just to document the fact that I didn't forget the copy constructor.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Back to top
Ian Collins
Guest





PostPosted: Sat Apr 21, 2007 6:10 am    Post subject: Re: Copy constructor question Reply with quote



E. Robert Tisdale wrote:
Quote:
James Kanze wrote:

Victor Bazarov wrote:

And there is probably no need for the copy c-tor as it's implemented.
The one the compiler will generate is absolutely sufficient.


Still, most coding guidelines would require an explicit one, to
avoid making it inline.


Victor is correct of course.
Actually, I would write

//A(const A& a): myvalue(a.myvalue) { }// copy

just to document the fact that I didn't forget the copy constructor.

That's silly, what happens when the class innards change? You have to

maintain the unused constructor.

If anything, write

//A(const A&);// copy

--
Ian Collins.
Back to top
xperthands
Guest





PostPosted: Sun Apr 22, 2007 9:03 am    Post subject: Re: assignment operator Reply with quote



On Apr 20, 12:41 am, James Kanze <james.ka...@gmail.com> wrote:
Quote:
On Apr 20, 3:53 am, xperthands <xpertha...@gmail.com> wrote:

1) That it is a bit of an overstatement. It would only be broken in
the sense of
exception-safety. Those concerns are not universal.

In what sense: that the concern for writing correct code is not
universal? If the class consists of only primitive types, which
cannot throw, then there's no need for the test; some would
argue that there's no need for the user defined operator= to
begin with.

Your overstating again. Your class may be composed of non-
primitives that also have nothrow guarantees. primitives are not
the only types that fit that.

Quote:
Bullshit.

I know you're a moderator, but such rude language is
offensive. I should think that moderators would be held
to a higher standard.

Quote:
It could only improve performance if most assigns
were self assigns; if not assigning to self, it slows things
down (slightly).

Again, your knowledge of the use of the type can make
the world of difference. Which was indeed my point.

Quote:
The fact remains that when looking at foreign code, a test for
self assignment is a red flag that the operator= is broken.

I still disagree with that it is necessarily broken. I also use
that as a sign to look more deeply at the class and the
assignment operator, but when doing a code review, I try
not to jump to conclusions. That can introduce
unnecessary tension into the process. That tenson can
lead to problems in your review process.
Back to top
Alf P. Steinbach
Guest





PostPosted: Sun Apr 22, 2007 9:11 am    Post subject: Re: assignment operator Reply with quote

* xperthands:
Quote:
On Apr 20, 12:41 am, James Kanze <james.ka...@gmail.com> wrote:

Bullshit.

I know you're a moderator, but such rude language is
offensive. I should think that moderators would be held
to a higher standard.

I disagree with James on the technical issue, that the statement to
which he replied was bullshit (that doesn't necessarily mean I agree
with you, but I certainly disagree with James here), but, /if/ I'd
agreed with James' reasoning, I may have used the same word.

We shouldn't be afraid to use what we think are accurate labels, short
concise language, just because someone might be offended.

It would be quite another matter to characterize a person, as opposed to
a statement, that way.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Back to top
Jerry Coffin
Guest





PostPosted: Mon Apr 23, 2007 9:10 am    Post subject: Re: assignment operator Reply with quote

In article <1177279599.705278.95150 (AT) q75g2000hsh (DOT) googlegroups.com>,
oldwolf (AT) inspire (DOT) net.nz says...
Quote:
On Apr 20, 5:06 am, James Kanze <james.ka...@gmail.com> wrote:

Since when? This restriction isn't present in C99, and it
certainly wasn't present in earlier versions. As far as I know,
C and C++ use exactly the same rules with regards to pointer
comparison.

The rules are the same for equality comparisons (namely, it is
always allowed). The rules for relational comparisons are different;
in C it is undefined behaviour if the pointers do not point to parts
of the same object (or one past the end), but in C++ it is
unspecified.

It's also worth mentioning that std::less gives (loosely) specified
results even when the operators don't.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Back to top
MC felon
Guest





PostPosted: Thu Apr 26, 2007 9:10 am    Post subject: Re: input problem Reply with quote

Sorry!
there was a problem with the server, i think.
anyway, i tried getline(). it's working with the cin.ignore() function
but there is a new problem.

[source]


getline(cin,ans);
if( ans == "yes")
{ //do something
}
else if(ans == "no")
{ //do something else
}
else
{
cout<<"invalid answer";
}

[/source]

when i type "yes" OR if i type "no", it says "invalid answer".
what's wrong?
Back to top
Tim Love
Guest





PostPosted: Thu Apr 26, 2007 9:11 am    Post subject: Re: input problem Reply with quote

MC felon <paec.nwa (AT) gmail (DOT) com> writes:

Quote:
getline(cin,ans);
if( ans == "yes")
...


Quote:
when i type "yes" OR if i type "no", it says "invalid answer".
what's wrong?
Well, I suggest you print out ans. Do it so that you can see if

there are trailing invisible characters - maybe
cout << "|" << ans << "|" << endl;
Back to top
Rolf Magnus
Guest





PostPosted: Sun Apr 29, 2007 4:22 am    Post subject: Re: Inheritance Reply with quote

dasjotre wrote:

Quote:
On 27 Apr, 11:31, Murali <nalajala.mur...@gmail.com> wrote:
Hi
I have 2 questions.
1) How do i make a class so that no other classes can't be derive from
my class?
2) Do i declare the constructor as private? Can any one explain when
exactly it is useful?

check FAQ
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.11

I personally prefer

class A // DON'T DERIVE FROM THIS !
..

I prefer letting the user of the class decide whether he finds it useful to
derive from it.
Back to top
osmium
Guest





PostPosted: Sun Apr 29, 2007 4:30 am    Post subject: Re: Inheritance Reply with quote

"Rolf Magnus" writes:

Quote:
dasjotre wrote:

On 27 Apr, 11:31, Murali <nalajala.mur...@gmail.com> wrote:
Hi
I have 2 questions.
1) How do i make a class so that no other classes can't be derive from
my class?
2) Do i declare the constructor as private? Can any one explain when
exactly it is useful?

check FAQ
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.11

I personally prefer

class A // DON'T DERIVE FROM THIS !
..

I prefer letting the user of the class decide whether he finds it useful
to
derive from it.

Which is exactly what was proposed in the message you're responding to.
Furthermore, it seems like good choice to me.
Back to top
Greg Herlihy
Guest





PostPosted: Mon Apr 30, 2007 9:10 am    Post subject: Re: Virtual inheritance Reply with quote

On Apr 29, 9:25 pm, "Massimo" <bar...@mclink.it> wrote:
Quote:
"jg" <jgu...@gmail.com> ha scritto nel messaggionews:1177906176.761735.176510 (AT) n76g2000hsh (DOT) googlegroups.com...

I think the object of Last will have a single subobject of Base,
not two. The other subobjects are not shared (i.e. there are
two subobjects each for D1Base, D2Base, Middle).

Base
/ / \ \
/ / \ \
/ / \ \
D1Base D2Base D1Base D2Base
\ / \ /
Middle Middle
| |/
D1Middle D2Middle
\ /
\ /
\ /
Last

I really don't know, but don't think so, since each and every one of these
classes is (somewhat) a subclass of Base, and so inherits (or seems to
inherit) the virtual inheritance setting.

I'm actually quite sure there's only one Middle object, because the compiler
doesn't complain about method names resolution issues... while it *does*, if
inheritance is made non-virtual.

Anyway, any clue on how to obtain the desidered behaviour (if this is at all
possible)?

The problem is that every class that declares a virtual base class
shares that base class object with every other class that has declared
the same virtual base class. Therefore it is not possible to declare
D1Base and D2Base in such a way that the two pairs of D1Base and
D2Base objects share the same Base object as one another - without
also having them share the same Base object as every other pair of
D1Base and D2Base objects in the hierarchy. In other words, it is
possible to reduce the top of the current inheritance graph to a
single Base object - or three or four Base class objects - but not two
- as the graph is currently structured.

One way to remedy this problem - and to have the two Base objects at
the top of the hierarchy as desired - would be to insert a pair of
"buffer" classes, Base1 and Base2, between the Base and the D1Base and
D2Base objects. D1Base and D2Base would then each inherit from both
Base1 and Base2 as virtual base classes. The rest of the current
inheritance hierarchy would remain the same.

To illustrate. With this change the top level of the inheritance graph
would like this:

Base Base
| |
| |
Base1 Base2
| | | |
| \ / |
| \ / |
| \/ |
| /\ |
| / \ |
| / \ |
| | | |
D1Base D2Base

And the revised class declarations at the top of hierarchy would look
like:

class Base
{
int n;
};

class Base1 : public Base
{
};

class Base2 : public Base
{
};

class D1Base : public virtual Base1, public virtual Base2
{
};

class D2Base : public virtual Base1, public virtual Base2
{
};

The rest of the inheritance hierarchy would remain the same.

Greg
Back to top
James Kanze
Guest





PostPosted: Mon Apr 30, 2007 9:10 am    Post subject: Re: array of pointers Reply with quote

On Apr 30, 12:22 am, Old Wolf <oldw...@inspire.net.nz> wrote:
Quote:
On Apr 30, 4:10 am, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:

Jess wrote:
Then "x" is a const char*[3], and can decay into const char**. I've
heard about a pointer pointing to the entire array, but haven't seen
an example yet. For my example, how can I get a pointer pointing to
the whole "x" array?

const char* (*pa)[3] = &x;

In addition, what can we use it for?

Not sure. I can't recall ever needing one.

I use them in functions that expect to be passed a fixed-size array,
e.g.
cryptographic functions:
bool des_cbc_checksum( byte (*out)[8], void const *in, size_t
in_len );

In most such cases, I'd use a reference to the array, rather
than a pointer (unless, of course, I had to be compatible with
C).

Quote:
If so, I guess it's the responsibility of this function to check (by
some method) if the argument is a pointer or not. Is this right?

No. The responsibility lies on the caller, in most cases. Pass the
size along and treat is an an array if the size > 0. Treat it as
a single object if the size == 0.

void foo(T* p, size_t s = 0);

Wouldn't it make more sense to use 1 as the size of a single object,
and have 0 be an error?

It depends. I can't think of a case where it would make sense
to have a parameter which can be either an array or a scalar,
but if it did, I'd probably use -1 as the flag for scalar, in
order to distinguish the case from an array with either 0 or 1
members.


--
James Kanze (GABI Software) email:james.kanze (AT) gmail (DOT) com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Back to top
Juha Nieminen
Guest





PostPosted: Mon Apr 30, 2007 9:10 am    Post subject: Re: Virtual inheritance Reply with quote

jg wrote:
Quote:
Base
/ / \ \
/ / \ \
/ / \ \
D1Base D2Base D1Base D2Base
\ / \ /
Middle Middle
| |/
D1Middle D2Middle
\ /
\ /
\ /
Last

If you are going to draw ascii art, it would be a good
idea to use fixed-width font, wouldn't you think? That looks
like gibberish when looked with a fixed-width font.
Using variable-width font to draw ascii art is insane because
it assumes everyone else is using the exact same font as you are.
Back to top
James Kanze
Guest





PostPosted: Mon Apr 30, 2007 9:11 am    Post subject: Re: Virtual inheritance Reply with quote

On Apr 30, 3:53 am, "Massimo" <bar...@mclink.it> wrote:
Quote:
Hi to all, I'm facing a problem in a particularly complex inheritance
hierarchy, and I'd like to know what the standard says about it and if my
compiler is correct in what it does.

I have two consecutive layers of diamond-shaped inheritance; the first layer
is declared as using virtual inheritance, while the second one is declared
as *not* using it.

This is what I'd like to have:

Base Base
/ \ / \
D1Base D2Base D1Base D2Base
\ / \ /
Middle Middle
|| ||
D1Middle D2Middle
\ /
\ /
\ /
Last

Which is impossible in C++.

Quote:
This is what the compiler actually does:

Base
/ \
D1Base D2Base
\ /
Middle
/ \
D1Middle D2Middle
\ /
Last

That's not what it does given the code you posted. What you're
actually getting is more like.

------Base------
/ / \ \
/ / \ \
D1Base D2Base D1Base D2Base
\ / \ /
Middle Middle
|| ||
D1Middle D2Middle
\ /
\ /
\ /
Last

Quote:
If I remove all virtual inheritance, everything goes just
fine; if I use it in the first layer, there seems to be no way
of disabling it afterwards.

Virtual inheritance only affects the classes which derive using
it. On the other hand, all instances of all classes which
derive from Base will share a common instance. You can't group,
with two or three different groups of classes sharing a common
instance.

What you might try is making Base, the two DnBase and Middle
templates (without changing anything else), and instantiating
them on eithre D1Middle or D2Middle. By doing this, the 2
Middle, each of the DnBase, and the two Base are formally
different types. This could result in significant code
duplication, of course.

Quote:
Does the standard saying anything about this, or is it
implementation-dependant? My compiler is Visual Studio 2005.

The standard specifies very precisely what abstract graph you
should end up with. All of the compilers I have access to
(VC++, Sun CC and g++) are conform in this respect, and
generate to diagram I've given with your code.

--
James Kanze (GABI Software) email:james.kanze (AT) gmail (DOT) com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Back to top
Søren
Guest





PostPosted: Tue May 01, 2007 9:11 am    Post subject: Re: Type conversion Reply with quote

Erik Wikström skrev:
Quote:
On 2007-04-30 21:09, Søren wrote:
Hi Guys

I'm still trying to learn C++, and it´s going im the right direction.
The only thing that keeps make me banging my head againts the wall, is
when I have a variable of some sort, and need to use its value in a
different type. :-|

Right now I have two wchar_t* strings (L"Hello World" and L"123"),
that needs to be converted into char* and int.

You really should be using std::wstring unless you can come up with a
convincing reason not to (and you'll have a hard time convincing me).


Well .. it´s basicly not my choice. The reson for having the conversion
troubles is that I use different librarys, which use the different
types: Irrlicht uses wchar_t* a lot and RakNet seems fond of char* - and
when I have produced a wchar_t string using some Irrlicht function and
needs to present it to RakNet, then the problems arise.

Quote:

Could someone please give me a hint on how to do this (need to work
with Mingw in Windows and GCC in Linux), and maybe explain where to
find usefull information for stuff like that, in the future.

Don't know about the first one, what would happen if there's no way to
represent the wchar_t as a char?


Well .. then I would have to re-think my code, since the wanted
functions return types and input types is the way they are.

Quote:

The second one on the other hand should be able adapt the solution from
the FAQ
(http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2):


#include <iostream
#include <string
#include <sstream

template<typename T
inline T wStringToAny(const std::wstring& s)
{
std::wistringstream i(s);
T x;
if (!(i >> x))
throw "Bad conversion";
return x;
}

int main()
{
std::wstring str = L"123";
int i = wStringToAny<int>(str);
std::cout << i << std::endl;
}
Back to top
Sylvester Hesp
Guest





PostPosted: Wed May 02, 2007 9:10 am    Post subject: Re: reference vs pointer Reply with quote

"Erik Wikström" <Erik-wikstrom (AT) telia (DOT) com> wrote in message
news:kJLZh.39878$E02.15987 (AT) newsb (DOT) telia.net...
Quote:
On 2007-05-01 19:19, Rolf Magnus wrote:
Erik Wikström wrote:

On 2007-05-01 18:21, sam_cit (AT) yahoo (DOT) co.in wrote:
Hi Everyone,

A reference is a alias for a variable.

int i =5;
int &b = i;

Is memory allocated for the reference variable like a pointer
variable?

No, try this:

int* ip = &i;
int* bp = &b;

if (ip == bp)
std::cout << "Same\n";

What does this have to do with the question if a reference takes up
memory?

Well, basically if it takes up memory then it has an address, and using
the address-of operator returns the address of it's operand, so if a
reference did take up memory then the address-of operator would return its
address.

Well, then how would you explain the following behaviour ?

struct A
{
int i;
};

struct B
{
int i;
int & j;
B() : j(i) { }
};

int main()
{
std::cout << sizeof(A) << std::endl;
std::cout << sizeof(B) << std::endl;

B b;
std::cout << &b.i << ", " << &b.j << std::endl;
}


- Sylvester Hesp
Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Goto page Previous  1, 2, 3 ... 16, 17, 18, 19  Next
Page 17 of 19

 
 


Powered by phpBB © 2001, 2006 phpBB Group