« first day (329 days earlier)      last day (4621 days later) » 

6:00 PM
Hint: If you come to C++ from a different language, forget everything you know about the terms "object", "reference" and "initialization".
4
 
@JohannesSchaublitb sorry I am just ignoring what you are saying..because I just don't understand that xD
 
@Als I don't understand why you are getting into tricks
 
@LewsTherin Formally it's equivalent to string& name(n); but that won't make a difference here.
 
@FredOverflow does that not mean I am declaring string &name twice?
as I have done that already in the class body
 
Als
@JohannesSchaublitb: What tricks? That was a compliment for you...no?
 
6:01 PM
@Als i think you want to trick me into believing it was a compliment
 
Als
@JohannesSchaublitb: Now you are trolling :)
 
@LucDanton ah yeah I see..
but to me it seems I am declaring name twice
 
@LewsTherin Oh, you meant that code literally. string &name = n; is how you declare and initialize a local variable one the same line, : name(n) is how you initialize a data member declared elsewhere.
 
@LewsTherin Where is the first time?
 
The first time will be under private
like private: string &name
then I call the member initializers which does the almost the same thing like what Fred said
string &name(n)
 
6:03 PM
@LewsTherin With C++11 you can put initializers in-class. But that won't work here since n is not in scope.
 
With C++11 you can put initializers in-class. But that won't work here since n is not in scope.
 
This is how you initialize reference data member:
class Dog
{
    std::string& name;
public:
    Dog(std::string& n) : name(n)
    {
    }
};
But note that you probably don't want a reference member in the first place. What you probably want is this:
class Dog
{
    std::string name;
public:
    Dog(const std::string& n) : name(n)
    {
    }
};
 
@LucDanton lulz
 
@JohannesSchaublitb Pirate!
 
@JohannesSchaublitb I have a strange sensation of déjà vu.
 
6:05 PM
@Fred, I do for the game I am working on..it has too many freaking arrows lol
 
@LucDanton i meant it as compliment -.-
 
As did I.
 
I just want to make my life simple by passing a reference hence the question
 
i wanted to take your good answer and life it one position up in the window :(
 
@LewsTherin You don't need an arrow to access a string stored by value.
 
6:05 PM
i would like to thank all who post Interesting Stuff here, so that I can repost it on my FaceBook. thanks. in particular Fred for whitenoise music, and robot for that Euler lecture video.
 
Arrows? In C++?
 
What whitenoise music?
 
What did I miss?
 
Nope, I'm not using a string..I am using user defined objects
 
@RMartinhoFernandes He probably means p->member or something.
 
6:06 PM
I just used string as an example
@FredOverflow yeah
 
Who skipped to arrows without going through monads first?
 
FaceBook. im scared
 
@LewsTherin Okay, so you want to access the same object from different places?
 
Exactly, so I create the object on the heap dereference it
 
6:06 PM
 
@LewsTherin So what type do you use in your real code instead of string?
 
I use new PlayerBeing(x,y,color...)
 
Or I pass in vectors
to the player object.
 
please use ` and ` to mark what is code and what is other text
 
6:07 PM
@LewsTherin Please don't store references to dereferenced pointers to heap objects. You would later have so say delete &ref;, and that's just not how we roll in C++.
 
but I would still have to say delete *ptr though
 
we cannot know whether "PlayerBeing(x,y,color...)" is new code or whether "new PlayerBeing(x,y,color...)" denotes the code in question
 
so what's the difference?
 
No, to delete an object you say delete ptr;
 
yeah sorry...it will free the memory ptr points to
as will delete &ptr ?
 
6:09 PM
yes
 
How do you make sure the PlayerBeing isn't deleted too soon? You probably want a std::shared_ptr<PlayerBeing> instead of a raw pointer or a reference.
 
delete &ptr will blow up most of the time.
 
but in that case it only works if &ptr can be deleted
 
Real code would help, have you already posted a link? If so, I missed it.
 
delete ref; can work too if there is a conversion function
but most probably it will do the wrong thing
 
6:10 PM
Why should it be deleted soon? I manually delete it when I call the delete world
 
T& ref = *(new T);   // <- PLEASE don't do that!
 
Yeah that's what I am doing
why?
 
Because that's not idiomatic C++.
 
lol damn, i'd have to refactor again damn
 
References are non-owning by convention.
 
6:11 PM
@LewsTherin it's against the principle of least surprise
 
Oh... ok
 
insofar "PLS don't do that"
 
Em... I just got lost..I mean since the object exists
 
2 mins ago, by FredOverflow
Real code would help, have you already posted a link? If so, I missed it.
 
6:12 PM
there shouldn't be any harm..or it is just a matter of convention of other programmers
@FredOverflow ok I will do that in a sec
 
If you're the client of the function void foo(T& t); nobody expects that calling foo(*some_pointer_to_t); will invalidate the pointer.
Or that T t; foo(t); will call delete on an automatic duration object.
 
same with void foo(T* t); foo(t)
otherwise, strlen and friends would all violate PLS
 
But they would see in the manual that a reference is passed
 
I suggest you learn C++ without using new, ever.
3
 
A C++ without a new? I can't imagine that lol
 
6:14 PM
@LewsTherin References are extremely limited by design. (They were put into the language to support passing by reference.) For example, if you later want to rebind it to a different PlayerBeing, you can't do that.
If you come from Java and you want a "Java reference", you don't want a "C++ reference", you want a "smart pointer" or a "raw pointer" (or no indirection at all.)
 
or reference_wrapper<T>
 
$ grep new include/ -Rn | wc -l
8
 
which is much like a java reference
 
@LewsTherin I'm sure @Luc is serious, and many of the regulars around here agree with him.
I do.
 
Some of them are also false positives (e.g. call to newtable).
 
6:15 PM
@LucDanton Yeah? What about src? :P
 
mmmn...i just thought that was right
 
$ grep new src/ -Rn | wc -l
1
 
@LewsTherin All those pointers, arrays and news indicate that you have been learning C++ from a very poor source.
 
OK so what's wrong with doing delete &ref
 
6:16 PM
That's a call to lua_newstate :)
 
Als
Okay I feel the need to post this again.
18 mins ago, by Als
@LewsTherin: You need a good book to learn C++, It can't be learned in a chat, not especially this one.
 
@LewsTherin nothing is wrong, but everything is worrying
 
@FredOverflow, but I need it though...for polymorphism
 
@LewsTherin So GameBeings is a polymorphic base class?
 
6:17 PM
Yeah
 
Polymorphism is available for automatic duration objects, too. But in any case, smart pointers are used for owning resources; not references.
 
you don't need new to use polymorphic class objects
 
Yes, I didn't know that until I asked Martinho
 
GameBeings *gameBeings[3];                                // C with classes
std::vector<std::shared_ptr<GameBeings>> gameBeings(3);   // C++
 
Might as well use std::unique_ptr.
 
6:19 PM
OK you're right I have to get a good book...because I don't know as much as I thought
Or as well
 
@LucDanton Yes, but that one is a lot harder to explain to newbies, because you have to understand exactly what an rvalue is.
 
I'd rather clone than share objects.
 
@LucDanton Depends on the desired semantics.
 
That applies to std::shared_ptr, too!
 
ok for example like in the code
 
6:20 PM
hm you still discussing c++
 
I had playerBeing(pos,vel..)
the thing is if I did Vector(3,4) and passed it into the constructor
 
here is new off topic: the long songs are not available on YouTube!
 
the temp object will be destroyed
 
at best they're split in 2, like
 
@Lews: I missed the beginning of the discussion. What is the real problem, does you code not compile? Does it do the wrong thing? Do you need to extend it and don't know how?
 
6:22 PM
and it sucks...that's why I am passing it by pointer reference
@FredOverflow The code was working fine with pointers before
But I felt that I didn't want to use"->" harder to read
 
@LewsTherin That's why you are supposed to copy the objects passed into the constructor instead of just binding them to a pointer or reference.
 
So I wanted to pass by reference instead to the objects and just access members using a simple "."
 
In C++, we love to copy objects, because it frees us from all sorts of problems related to lifetime and exceptions.
 
So what I should do is use a copy constructor
 
Of course, that doesn't work for polymorphic objects, but your Vector is not polymoprhic, is it?
 
6:24 PM
In C++ you have to carefully consider who or what owns what objects. That's a feature.
 
no the vector isn't polymorphic
you are right..i should've done that
 
Can we see the data members and constructor of PlayerBeings?
 
ok, a sec please
I will just paste GameBeings...because it is built from that?
 
I think you should stay away from reference members. They're quite exotic for C++ IME.
I think I have one class that uses them and it does arcane(-ish) stuff.
 
@LucDanton also const members
 
6:27 PM
Just to give you an example of what is wrong with raw pointers: when if(!allegro_init()) fails, the gameBeings array contains garbage pointers, but you still execute the delete gameBeings[i] ; loop, resulting in undefined behavior.
 
Yeah, for the same reasons.
 
@FredOverflow you are right... I wasn't doing any validation yet
I just wanted to get something running..mainly to learn trig.
 
@Lews Get rid of that & in your data member declarations and constructor parameter types, and then pass a Vector(x, y) instead of a *new Vector(x, y) and all should be fine.
 
@LucDanton references were introduced to C++ I don't see why we can't use it if it makes life simple...I don't understand what you guys mean by rebinding though
Oh jeez, I'm a sap..I see it now :)
 
int i = 42;
int j = 0;
int& r = i;
r = j;
@LewsTherin What do you think the fourth line does?
 
6:30 PM
@LewsTherin They're not used as members because they don't make life simple as members.
 
@LewsTherin Also, remove the destructor.
 
@FredOverflow it changes the value of i...not what it points to...of course
 
@LewsTherin Exactly. Impossible to rebind references.
 
Assume struct foo { int& member }; foo f = /* magic */; foo g = /* magic */;.
What does f = g; does?
 
Lol, I suppose you have to do a lot of C++ to understand..makes perfect sense why not to use it..although it should be fine if you intend the reference to be constant
 
6:31 PM
Also, defining PI with 20 decimal places is a bit optimistic, isn't it? :)
 
I couldn't get more out of google :(
 
@LewsTherin C++ references are like the ref keyword in C# ("call by reference"), but not the reference concept in C# ("reference semantics").
@LewsTherin The double data type only has 16 decimal digits. Anything after that is noise.
 
I used #define
would it truncate the data?
so i need a const long double or something ?
 
Show me a context where you use PI. I'm sure you are using double arithmetic.
Believe me, for a game, even a float with 7-8 decimal digits would be fine.
 
@FredOverflow Also it's a double literal.
 
6:34 PM
` float x =-cos(this->angle * PI/180) ;
float y =-sin (this->angle * PI / 180) ;
vel.setVector2(Vector2(x,y)) ;
pos.x = pos.x +vel. x;
pos.y = pos.y + vel.y; `
 
Ah, you pass it to sin and cos who expect doubles, there you go :)
@LucDanton Also, what Luc said.
 
so it is fine then? PI is a double by default then..?
unless you add a f
at the end
 
Yes, literals with a . and without the f are double, which gives you approximately 16 decimal digits. Again, for a game, that is more than fine.
 
ah so I will need to remove the extra I have there..cool :)
Anyone up for teaching me some trig? @FredOverflow are you? :P
I don't know why it happens but the rule always changes
to find the x and the y direction vector..
 
You don't have to remove them, you should just be aware that it does not buy you any more accuracy.
3.1415926535897932385 and 3.141592653589793 are two literals for exactly the same value.
 
6:39 PM
Are you sure? You can never be sure with FP stuff. :P
 
It can't be the same..? I mean some precision is lost
 
double x = 3.1415926535897932385;
double y = 3.141592653589793;
std::cout << std::boolalpha << (x == y) << std::endl;
 
Oooh, time for some Cppshell action!
 
@RMartinhoFernandes You can be sure if a) you know that your implementation uses IEEE754 and b) you know how IEEE754 works.
 
6:41 PM
> true
 
that can't possibly return true..i was told this in first year
which is why i do (x>=y)
 
The person you taught he didn't know what he was talking about then.
IEEE754 isn't something random and magical. But many people seem to assume that.
 
damn college, everything i've learnt has been wrong so far :( dang
I did learn ieee754 but just how it is stored and how to convert them
 
And to completely blow your mind, both x and y have the following value:
3.141592653589793115997963468544185161590576171875
That's as close as IEEE754 gets to PI.
 
aargh...:O
 
6:43 PM
Note that everything after the first 16 digits is noise (meaning those digits couldn't be anything else given the first 16 digits), but nevertheless, those digits are there.
 
So the data is stored in the memory location but they aren't retrieved?
 
The data is stored in binary, and when you convert that to decimal, you get funny looking long values.
 
10 not being a power of 2
(or something)
 
mmn..wow
so much to learn
btw, anyone ever worked with allegro here?
 
Well so far that's been a perpetual state of mind for me :)
 
6:47 PM
Internally, PI is stored as:
0100000000001001001000011111101101010100010001000010110100011000
Of course, you have to understand IEEE754 in order to interpret that.
 
but 1 means it is negative?
and pi isn't negative
the leading 1
 
@LewsTherin I initially only showed 63 bits instead of 64, corrected now :)
 
i dont have time to convert that though xD
the mantissa and the exponent
sigh
but what should i be able to deduce looking at that
 
The exponent is 2^(1024-1023) = 2^1 = 2, if I'm not mistaken.
Which makes sense, because it allows to represent numbers between 2 inclusive and 4 exclusive, which fits PI nicely.
 
yeeah but what does the representation mean..or what should it mean to me?
How can I apply it to a game
or any data..
 
6:51 PM
You were asking about double, weren't you?
 
You shouldn't really need to bother much about it.
 
yeah
 
You can do everything in radians and ditch Pi.
 
or floats
ha ha @LucDanton I am a mess with simple degrees imagine with radians
And it is allegro...I wouldn't know how to proceed quickly
 
@LucDanton Ah, you wish. Allegro uses a circle of 256 units.
 
6:52 PM
@RMartinhoFernandes Isn't that a bit imprecise?
 
!!!! I can't declare an array of reference??
 
@LewsTherin I dunno it's all proportional to me. 360 isn't any much better than 2Pi, is it?
 
@FredOverflow Why would it?
 
@LewsTherin No, because arrays are sequences of objects, and references aren't objects.
 
@FredOverflow he is right
 
6:53 PM
@LewsTherin References aren't object and thus have no size.
 
Again, you don't want references in C++ nearly as much as you think you do.
 
So the only way to use polymorphism then is using pointers
 
9
Q: C++: why can't we have references to references or array of references?

skydoorI noticed that there is no reference to reference but there is pointer to pointer, and also there is no an array of references but an array of pointers. Could anybody give me any reason?

 
A reference is like a pointer and you can store pointers ..
 
8
Q: Why can't we create array of references?

Mihran HovsepyanC++ Standard 8.3.2/4 says: There shall be no references to references, no arrays of references, and no pointers to references. But I can't understand why this restriction is added to c++. In my opinion the code bellow can easily be compiled and work? What is the real cause of this restr...

@LewsTherin No, a reference is not like a pointer. If you want pointer semantics, use pointers. References were never meant to replace pointers.
 
6:54 PM
@LewsTherin Like. Not the same.
 
like doesn't mean identical to.
 
Fair point
the answers make sense ha I see
so I have to use pointers then..freaking hell
 
@RMartinhoFernandes Because it's only 8 bit. Even my oldest games in the 90s used 1024 units for a circle.
 
@FredOverflow No, it's not 8 bit. Allegro uses 32bit fixed precision, as 16.16.
 
@LewsTherin What's so bad about pointers? Do you really care that much about one character more in your source code (-> vs. .)?
@RMartinhoFernandes Didn't you just say 256 is a circle?
 
6:57 PM
lol I don't find it too bad..just that it is nearly unreadable xD
 
@FredOverflow Yes, but you can represent 100.1 units if you want.
 
Or not as neat
 
@RMartinhoFernandes oic
Every C++ programmer understands the p->member syntax.
 
@FredOverflow there is also a problem when I get the cos and sin direction as you saw in my code
 
Again, why do you insist on using references? Are you a Java or C# programmer?
 
6:58 PM
WTF hg! "Not trusting file /path/to/repo/.hg/hgrc from untrusted user root, group users"?
 
No, I find it hard to learn Java xD
 
@RMartinhoFernandes That's a recent security feature IIRC.
 
@LucDanton How come it doesn't trust root?
 
I just find that it is easier on the eyes
 
@LewsTherin So you turned to C++ in the hopes it would be easier to learn than Java?
 
6:59 PM
Oh wait, that's not trusting one file, not the whole repo. I don't know what that is.
 

« first day (329 days earlier)      last day (4621 days later) »