« first day (375 days earlier)      last day (4566 days later) » 

12:00 PM
@BPDeveloper If Dog has a proper copy constructor, it should work fine.
 
and what is a copy constructor
 
user34537
typically i will create the object on the stack (plainly TheClass thevar;) and pass it by reference. Or i pass a deque/list/something which holds more then 1 object and i typically pass it by value (instead of pointer). But.... that might not give you a great performance but its so easy to use and you wont get leaks (cause you dont use the new keyword either)
 
@BPDeveloper do you have a textbook?
 
A copy constructor is a constructor that looks like Foo(Foo const& that). It is called when a copy of a class is required.
 
user34537
copy constructor is kind of like an assignment. They are usually interchangeable. When you do Foo foo=anotherFoo; either the copy constructor or assignment operator kicks in
 
12:02 PM
Er, no.
 
@acidzombie24 No, that's initialisation. Only the copy constructor is involved.
 
Don't write a copy constructor if shallow copies are ok

If the object has no pointers to dynamically allocated memory, a shallow copy is probably sufficient. Therefore the default copy constructor, default assignment operator, and default destructor are ok and you don't need to write your own.
 
Foo foo = other; is equivalent to Foo foo(other);, assignment operator never kicks in here.
Foo foo; foo = other; is assignment.
 
@BPDeveloper That's correct. It looks like the complement of the Rule of Three. In your case, you will have a pointer to a dynamically allocated Animal.
 
Also you can't express copy with assignment, so they're not interchangeable.
 
user34537
12:04 PM
The assignment op would if other isnt foo or doesnt fit the copy constuctor (ie it only accepts nonconst)
 
@acidzombie24 No, assignment only happens if it isn't initialisation.
 
It could be an implicit conversion, but not assignment.
 
Foo f = x; never invokes any assignment operator (other than those invoked inside constructors, obviously).
 
user34537
hmm. interesting. I should check that out one day soon
 
user34537
@BPDeveloper have you checked out parashift.com/c++-faq-lite?
 
12:07 PM
So if I want to assign dog to animal, then the functions from dog is lost
can't you cast this or something?
 
Hi
 
0
Q: how to store and reference large amount of data in c++

cateofI am using a vector of strings in order to store some data. More precisely an array of vector of strings. A simple scenario: I need to store the surnames of 256 cities vector<std::string> city[256]; A new requirement came to create a new "class Person" that will hold more data per item ...

 
@BPDeveloper You need to use a pointer or reference to Animal to get polymorphic behaviour.
 
Just a quick question regarding move constructors : once I define a move constructor for my class, shouldn't the default copy constructor be deleted automatically ?
 
Array of vectors is a new one.
 
12:09 PM
i must confess i don't know of any city that has a surname. should i assume that merely because i don't know about any, that the question is rubbish or sails under false flag?
 
Move and copy ctors are orthogonal.
 
@RMartinhoFernandes Only way
 
You can have both.
 
@BPDeveloper Yes.
 
Or none. Or either one.
 
12:09 PM
Animal* animal;
then animal = &dog;
 
You probably always want to have move enabled.
 
@BPDeveloper Here's a quick example: ideone.com/6AVzY
 
So isn't this accepted answer a bit wrong ? stackoverflow.com/questions/4782757/…
Destructor, move constructor, move assignment operator (in this case the class will not be copyable, useful for resource-managing classes where the underlying resource is not copyable)
Perhaps I'm getting this wrong.
 
It's not wrong.
You can have moveable but not copyable classes.
 
12:10 PM
@ereOn Some classes are movable but not copyable. std::unique_ptr is a famous example.
 
My understanding was that once you define a move constructor, the default copy constructor is deleted, but that you had to re-enable it if you wanted to still have it.
 
Dunno.
I don't remember the default generation rules.
I just make everything explicit now.
 
You can always force it with = default.
 
Well I wish I could :
Visual Studio 2010 doesn't allow it apparently
not yet at least
 
> If any move, copy, or destructor is explicitly specified (declared, defined, =default, or =delete) by the user, no move is generated by default. If any move, copy, or destructor is explicitly specified (declared, defined, =default, or =delete) by the user, any undeclared copy operations are generated by default, but this is deprecated, so don't rely on that.
 
12:12 PM
still have to declare my deleted defaults private
 
Or just suck it up and write it.
 
VS2010 is not entirely reliable in terms of C++11 support.
 
VS2010 supports lambdas and auto, and that's about it.
 
Ok, seems more clear to me now. Thanks to everyone.
I will just declare it explicitely then.
 
@CatPlusPlus Old-style rvalue refs too.
 
12:13 PM
It also has std::bind and std::function
 
Variadic templates. ;_;
 
VS2012 will support old-style rvalue refs with a bug fix. Ridiculous.
 
@RMartinhoFernandes What's the difference between old-style and new-style?
 
user34537
You guys are completely right. I do get an error on this code ideone.com/PhAwG it will not call the regular ctor and do the assignment op. It will just cause a a compile error
 
@CatPlusPlus Dunno. Just know that they implement rvalue refs from an older draft, so their behaviour is not exactly as it is supposed to be.
 
12:14 PM
Should I care about something regarding move semantics so ?
(I basically had to reimplement something similar to std::thread because it doesn't provide it)
 
@ereOn Most of the behaviour is fine. It's just a few details that don't add up.
 
Ok. Thanks.
 
@acidzombie24 It's a common point of confusion because that style of initialisation also uses =.
 
Would Animal animal; in header and animal = Dog(); work?
 
No. That will slice the Dog.
 
12:15 PM
The worst thing with C++11 is that when you start to get used to some nice features, it is really hard to go back when maintaining older programs :/
 
but would it have the fields from dog ?
 
@BPDeveloper No. That's what slicing means.
Unless you use pointers or references, you can forget that kind of polymorphism.
 
user34537
@ereOn: yeah. I dont want to go back :(
 
@ereOn What older programs? There are no older programs.
 
But then I don't understand how the reference whatever survives
 
12:17 PM
@CatPlusPlus You obviously don't work where I work :D
 
metalog is taking 50% of my server's CPU.
 
If I have Animal* animal;
 
It now clocks at 125 CPU hours.
 
then in some function assigns animal = &;
and the dog was made in the functions
 
You mean animal = &dog;?
 
12:17 PM
function*
 
user34537
I wonder why it wouldnt use assignment with a constructor but it will use copy constructor as an assignment ideone.com/3xhad
 
yes*
 
Well, once the dog goes out of scope, the pointer is no longer valid.
 
That is my problem
 
So, it's something to avoid.
 
12:18 PM
How do you do this
 
You need to use dynamic allocation (i.e. new).
 
@acidzombie24 That's not a copy ctor.
It does implicit conversion, and then assignment.
 
but when this function may be called again, what do you do with the old dog?
 
I.e. f = Foo(b)
 
@BPDeveloper I like how you think. You're hitting all the right walls :) You need to delete it.
 
12:20 PM
@bddeveloper, I really would recommend you reading a good book. that will help quite a lot.
 
No, really, metalog, what the hell are you doing.
 
so the function would look like this: delete animal; animal = new Dog();
 
@BPDeveloper That's an option.
 
Also, I don't know where you live, but slicing dogs in forbidden by law in some countries
 
But like I said before, it would not work nicely in the presence of exceptions.
 
12:21 PM
what happens with the last assigned dog when the program exits
then the OS remove it from memory?
 
Modern OSes reclaim all resources from terminated programs.
 
@BPDeveloper In a well-behaved program, it is deleted as well, by a destructor somewhere.
@CatPlusPlus Not all resources. You can hold remote resources for example.
 
so the class that has this animal pointer should have a destructor with delete?
delete animal *
 
Well, all resources it can.
 
@BPDeveloper Yes.
 
12:22 PM
This kills the dog.
 
@CatPlusPlus Dogs are cheap to create. new Dog();. There you go, I just leaked one.
 
Be glad @DeadMG is not around.
 
Now that dog is free!
 
But you didn't free it, you lost it.
 
sbi
@CatPlusPlus Good then you didn't summon him.
> To count oneself as an atheist one need not claim to have a proof that no gods exist. One need merely think that the evidence on the god question is in about the same state as the evidence on the werewolf question. - John McCarthy
 
12:25 PM
Summoning requires saying their name three to five times.
 
@sbi Have you read his "Robot and the Baby" story?
 
There's possibly a mirror involved, too.
 
@CatPlusPlus So what? Now it can roam wherever it wants.
 
user34537
@CatPlusPlus What do you mean "It does implicit conversion, and then assignment.". and "That's not a copy ctor". It shows that its running the copy ctor and there is no assignment op for both of those classes....
 
@acidzombie24 A copy constructor has either the form T(T const&) or the bastard T(T&).
Anything else is just a regular constructor.
Any constructor that can be called with a single argument can be used in implicit conversions (unless marked explicit).
 
user34537
12:36 PM
oh shi... oops. I dont know what i was thinking
 
user34537
its a regular constructor
 
The assignment operator is generated automatically by the compiler.
 
user34537
i dont know why i labeled it as copy
 
user34537
ok to be clear we are talking about this code?? ideone.com/3xhad
 
user34537
i was saying...
 
12:37 PM
Yes, I am.
 
user34537
i am suprised it generated an assignment op which uses a ctor. But it wouldnt generate a ctor to use with a compatible assignment op
 
user34537
alright cool. i see what you guys are saying. I dont like the technical term but it does make sense
 
It generates a copy assignment operator, like Foo& operator=(Foo const&)
The line f=b calls the conversion constructor Foo(Bar const&), and then calls this operator=.
 
user34537
Yes so its two steps (Foo(Bar const&) then operator=) for this code and compile error for the other thing i thought would be valid (f=b when foo doesnt have a bar constructor but has assignment). I just think thats weird and surpising. thats all.
 
Implicit conversions are nasty. I wish they weren't the default.
They happen behind your back.
 
user34537
12:44 PM
haah yeah i guess
 
user34537
do you by chance remember a bad story?
 
I don't have that much C++ experience.
 
user34537
I havent had a problem with it but so far i only done it for 1) personal stuff or assignments in school 2) really small simple stuff at work
 
But I'm already in the habit of making constructors with a single argument explicit.
@acidzombie24 I only ever used C++ for personal stuff. There's no C++ in my college course.
 
user34537
Why do you use C++ instead of C#?
 
12:50 PM
I'm learning it.
 
acidzombie2, C++ have performance advantages on C#
 
user34537
ah. I dont bother with C++ unless i really need to
 
@PavelRyzhov That's not why I use it.
 
user34537
ie: It takes several seconds but likely minutes to execute
 
Also, don't let the Cat hear you say that.
 
user34537
12:51 PM
which is only two projects so far
 
> Languages don't have speeds.
I'm enjoying C++ so far. It has a lot of crappy baggage from the olden days (specially from C) but I can put up with it (plus I have C experience).
 
user34537
lol. But languages do allow you to not have garbage sollection thus you can get more speed :p
 
I really like the templates, even though they're baroque.
@acidzombie24 Or less.
 
I love C++ and it is, realistically, my language of choice. However, C# pays my wages and there are some things in C# I'd like to see in C++. Actually, I like them both equally.
 
user34537
I havent been convince of "or less". Just use a different allocator in that case...........
 
12:53 PM
@MooJuice Me too :)
@acidzombie24 See, and while you do that, I'll be off writing some more code.
 
user34537
Well realistically you would never need that extra speed
 
ROFL
Ever seen what a garbage collector does to an XNA game's frame-rate?
 
user34537
(and thus why i use C# most of the time lol)
 
user34537
hahah yeah
 
That's why I don't really like to discuss this performance thing.
 
user34537
12:55 PM
I wrote a custom allocator ONCE
 
user34537
it was SO INCREDIBLY FAST
 
Whilst it may be true that there are many things in C# and C++ performance-wise that bare no need for discussion, the garbage-collection idiom is a serious issue in games.
So sometimes it is best not to say "realistically" when you actually mean "sometimes".
 
The only measure of performance that matters is fast enough. Not faster, not slower, not super fast, not super slow. Just fast enough.
 
@RMartinhoFernandes int x; x; // here the second statement results lvalue but is it the case compiler returns reference to x etc , or what?
 
R. Marthiho: Exactly
 
user34537
12:56 PM
downside was you can only use it for one task. A task took several seconds/minutes so it isnt a big deal to batch them serially using multiple processes. But the speed was so good on it lol. It cut time down by 1/8th :)
 
@MrAnubis The compiler will probably just optimize the x; line away.
 
If your game is running at an acceptable frame-rate, case closed. If the user's aren't complaining that their application is taking too long to run an invoice-run, case closed.
 
@RMartinhoFernandes forget about optimization please , tell me concept?
 
@MrAnubis You asked what the compiler would do!
 
We should strive to write code that is high-performance, but we shouldn't lose sleep over it until it becomes a real issue.
 
12:58 PM
That line won't do anything.
I'm not even sure if it does something on the abstract machine.
 
@RMartinhoFernandes i am asking in terms of lvalue , what compiler does , or think of x=2;
 
user34537
string s1, s2, s3;
s1 = s2 - s3;   // oops, probably meant "+"
 
@MrAnubis That returns an lvalue, yes.
 
user34537
If string had a implicit conversion to char* this would work. But... How the F%^&* does the compiler know to convert it in that case?
 
@RMartinhoFernandes and what does returning lvalue means exactly?
 
12:59 PM
As a developer, one shouldn't be asking "will using boost::shared_ptr<> give me a performance hit?". We should use best-practices, and only if performance suffers and is directly attributable to a best-practice, should we consider our next move.
 
@MrAnubis You know the difference between lvalues and rvalues?
 
I'll shut up now :D
 

« first day (375 days earlier)      last day (4566 days later) »