« first day (344 days earlier)      last day (2500 days later) » 

6:27 AM
Are virtual pointers inherited?
in derived class from base class given that they are polymorphics
 
@cbinder What do you mean by "virtual pointers"?
 
3
Q: Mechanism of Vptr and Vtable in C++

Blue DiamondIn C++, during dynamic binding, consider the following example... class Base { virtual void fun() { cout<<"Base"; } }; class Derived : Base { void fun() { cout<<"Derived"; } }; int main() { Base *bptr; Derived d; bptr=&d; bptr->fun(); } The output of th...

 
7:13 AM
can anybody reply on this?
 
nwp
7:38 AM
@cbinder According to the standard there is no such thing as a Vptr or Vtable. It just says that when you call a virtual function on a base class and the object's dynamic type is derived from that and overrides the virtual function then the derived function will be called. How that happens depends on the compiler's implementation for virtual functions and there is no way to find out besides reading source code or documentation for the compiler.
 
 
1 hour later…
8:49 AM
@nwp ok..accepted. But vptr/vtable is common way how dynamic dispatch is being implemented by most of the compilers.
So Qn is legit
 
nwp
In theory a derived objects must contain a vptr so that when a base pointer points to it it can find the overridden functions. In practice compilers are good at optimizing and might devirtualize the whole thing and optimize out the vptr, vtable and indirection completely.
The best you can do is make an example where that matters and then check if the generated assembler code is good enough for your purposes.
 
9:54 AM
#include <iostream>

using ch_t = unsigned char ;

void foo ( ch_t volatile ch )
{
printf( "%d" , ch ) ;
}

int main() {

volatile ch_t prev = 224 ;
volatile ch_t over = ( prev + ch_t( 2 ) ) ;

foo ( over ) ;

return 0;
}
i'm getting 226.
with O0
 
Ven
why are you using volatile all over the place, please?
(you can use ctrl-K to indent your code)
@GreenTree the answer is that you return an int, but your error code is like 255 at most, so it underflows.
 
where exactly i'm returning int.?
 
If I have a function that returns int** and I return dynamically allocated array (in that function) of pointers to int. Will that array be returned by value (i.e. will it be copied when returned)?
 
Ven
ah, sorry
I thought you were returning over from main
@MuhamedCicak only the pointer is copied
 
@Ven Which one? I have array of pointers.
 
Ven
10:01 AM
@MuhamedCicak you have a int**, that is copied.
the address to the beginning of the array
@GreenTree what's wrong with 226?
 
nothing is wrong with 226, but with 0 it would be also ok.
 
@Ven Lets say that function (in which i allocate array and return it) is in some class. And I deallocate (i.e. delete) array after I return it from the function, it would not affect the returned array, right?
 
Ven
@GreenTree 224+2 = 226, that sounds correct to me..?
@MuhamedCicak some code, please
 
@Ven Alright, just a sec.
 
using ch_t = unsigned char ;
 
Ven
10:04 AM
@GreenTree yes?
226 fits in an unsigned char.
 
std::numeric_limits< unsigned char >::max() == 225 doesn't it on platforms where CHAR_BIT == 8.?
 
Im sorry, how can I paste code normally here (i.e. formatted)? Whenever I press enter it sends the message rather than entering new line.
 
Ven
@GreenTree std::numeric_limits< unsigned char >::max() = 255
225 just doesn't make any sense.
@MuhamedCicak I recommend coliru.stacked-crooked.com
 
omg
i had just copy pasting the literal all of time
.!
sorry.
 
@Ven I can explain like this without code. I have a func which allocates int** array and returns it. So, it is automatic variable in that func, so when code exits func , the array will be deleted. So because it is deleted, will it be usable there where you returned it? I mean, if it is copied, it should. Right?
 
Ven
10:14 AM
> the array will be deleted
why would it be?
if you new'd it, or malloc'd it, it won't.
 
Omg, i just missed the part where i create dynamic memory
sorry
The question I want to ask is, if I even deallocate (delete) the array. Will it be usable again on the other side where you returned it? Because it is copied, or is it?
Thats what i wonder.
Sorry if i couldnt explain. Because, I usally am not good at it :)
 
Ven
no, it's not. The only thing that's copied is the pointer itself, but it's only an address.
The array has already been deleted.
 
So you get pointer on that address?
I mean, the returned thing, is pointer to the array, not the array itself?
 
Ven
yes.
 
How could I return the array itself?
 
Ven
10:18 AM
arrays aren't pointers.
 
The thing is, I want to return the array by value? Do you get what I am saying (I may not say it correctly)?
 
You can't
 
Ven
I think you don't want to return the array by value, tbh.
 
make wrapper.
or so.
std::array.
 
Ven
use a vector...
 
10:21 AM
The point is, each time I call function it will allocate array, put some information in it, and return it.
 
Ven
so don't delete the array.
 
Then use a vector or an array if you know the size at compile time
That's everything there is to it
 
Okay.
 
Use a vector and stop worrying about what's copied or not
 
The thing is, I did not learn what vector is yet, nor did I learn c++ fully. I never used vector. I am in learning phase of C++.
But, I will try to use it.
 
10:23 AM
A vector is a dynamic array
 
10:41 AM
hey
need quick answer
what's the best way to check if 2 objects are same
Object a = ...
Object b = ...
 
Ven
what language is that?
 
if (a == b)
or
if (a.getId() == b.getId())
C++
Object is my own class
every object has an ID
 
Ven
both don't have the same semantics
depends what you operator== does.
 
what do you mean by that?
 
Ven
a == b calls a.operator ==(b)
(ADL notwithstanding)
 
10:43 AM
yup
IDK, I didn't implement my own == operator
 
Ven
if you didn't, then a == b won't work.
 
sorry, I forgot to add pointers
Object* a = ...
Object* b = ... (assume it's same `a`)
 
Ven
ok.
so you compare a == b with a->getId() == b->getId()
 
Ven
still doesn't have the same semantics. Two objects can have the same id but a different address.
 
10:45 AM
I think it doesn't matter
 
Ven
it matters as my previous message explains
 
why?
if b is a it must have the same address
or am I wrong?
 
Ven
you are wrong
```
Object* a = db.find(1);
Object* b = db.find(1);
a == b; // false
a->getId() == b->getId(); // true
 
Ven
in this case, yes
both will be correct
"Object* b = ... (assume it's same a)" here same doesn't mean anything
so yeah if you just stored the pointer into another variable
go for a == b
 
10:48 AM
kk
ok, but how the pointer comparsion works?
does it just ONLY compare the address?
or it does something else?
 
Ven
it only compares the address.
but more generally, you're not allowed to compare pointers.
 
ok, thanks for help
 
Ven
you're usually only allowed to compare pointers if you know both are stored in the same contiguous area.
so, carefully.
 
yep, you're right
 
Ven
11:34 AM
ah no sorry, it's only for <> etc
 
I have two classes: A and B. B inherits from A class. When I have function A* foo(), I can do something like this, it works: dynamic_cast <B*>(foo()); But I have function like this: vector <A*> foo(), and when I do: dynamic_cast <vector<B*>>(foo()); I get error: invalid target type for dynamic_cast
 
Ven
Variance
 
How do you mean?
 
Ven
16
Q: Container covariance in C++

Daniel GehrigerI know that C++ doesn't support covariance for containers elements, as in Java or C#. So the following code probably is undefined behavior: #include <vector> struct A {}; struct B : A {}; std::vector<B*> test; std::vector<A*>* foo = reinterpret_cast<std::vector<A*>*>(&test); Not surprisingly, ...

 
So it is impossible, as I understood. I hope I did not understood very well..?
 
Ven
11:42 AM
It's impossible indeed.
 
vector is a mutable container, such casting would break type safety if it was possible
even though derived is-a base, vector of derived is not a vector of base
 
So there is no possible way of having dynamic_cast on vector of any type of pointer?
 
Ven
no. you'd need to create a new vector and dynamic_cast every member one by one
 
dynamic_cast operates on pointer or references
^ on why such casting would be type unsafe
 
Could it be done with templates (i.e. generic programming)?
Maybe I could pass the type (to foo function) I want it to be (in this case B). And in the foo function I may create vector of that type and then return it. Than, I wouldnt even need to use dynamic_cast
Is it an option?
Maybe foo shouldnt create vector of A* at all. But the type I pass to it (in this case B*), thats what I am trying to say.
And return the type I pass to it (the function foo), so it would return B*. Is it possible to be done like this? I am quite new to generic programming.
Never mind, I think it can be done as that, but I may explained it wrong (most probably). So, yeah, never mind...
 
Ven
12:08 PM
it can be done, I'm not sure I'd recommend that tho.
(discards any element not of that type)
 
 
7 hours later…
7:21 PM
@Ven Im sorry, I am not sure I understand your code. I am really really new to generic programming. I made it another way, and it works (it may not be good though). I will paste code here when I come home, i need to go now, im in rush.
 
 
1 hour later…
8:50 PM
@Ven Im sorry, I failed making example of it, its just too messy...
@Ven I could paste the actual code if you want ?
 
9:12 PM
for (std::map<std::string, std::vector<std::string> >::iterator i = mahmap.begin(); i != mahmap.end(); ++i) {
std::cout << i->first << " : ";

std::vector<std::string>::iterator it;
for( it = i->second.begin(); it != i->second.end(); ++it) {
// process it
std::cout << *it << " ";
}

}
why do we need * at it
(this is c++98)
if i compile it with c++11 but without removing the asterisk will it make an actual difference? (without changing anything else)
 
@AnnaK. it is an iterator. It acts like a pointer (pretty much). You need to dereference it (i.e., *it) to get the thing it refers to. If you try to print out it itself, it either won't compile at all, or (e.g., with some really old implementations of vector) it might print out addresses of items in the vector rather than the items themselves.
As far as C++98 vs. 11 goes: they should work the same for this code.
 
thank you @JerryCoffin
 
@AnnaK. Surely.
 
9:55 PM
Hey, quick question, which code is more easily readable?
#1
#2
or more easy to understand for humans to read?
 
6 messages moved from Lounge<C++>
 
what do you think @milleniumbug?
at first glance?
 
nwp
@faceless both are pretty bad with tons of duplicate code which could be easily avoided
 
both are bad because you can refactor the pluralization into a function
 
Show meeeee!!!
please?
for
sorry
can you show me for pastebin #2
thats my code QQ
 
10:00 PM
and store individual counts and nominal values in an array
 
hmm okay
 
@faceless Writing the code. Just a sec.
 
10:22 PM
Hello World
 
@faceless Here: pastebin.com/5b0Nu1rW
@faceless You need to compile it with: g++ -std=c++11
 
Could I ask for a small code review? :)
 
@faceless This is just small refactor, you can put those double's in array of double's as @milleniumbug said.
@Toreno96 The code?
 
@MuhamedCicak pastebin.com/fChJk9ie
There're questions in comments.
 
10:26 PM
@milleniumbug Yeah, nice code :)
@Toreno96 I am looking at it.
 
@MuhamedCicak Ok, thx :)
 
@milleniumbug Sorry, @milleniumbug can help you I guess. I am not a C++ expert.
@Toreno96 I mean, I am learning C++.
 
delegating invariant checking into a separate function is a good thing, but you're not doing it right really
after the thrown exception, your class is in invalid state (that is, it's not a digit)
also your solution violates LSP
I'd say I wouldn't use inheritance for this
implicit conversion from Digit to Integer would probably be a better choice
 
Guys how can I delete a specific line from file or array in c++? I can't figure it out the data is like this
1 go check computer
2 get back home
 
@milleniumbug Inheritance shouldn't be combined with Boost.Operators, did I understood well?
 
10:32 PM
So if the user enters 1 it deletes the line
Anyone here who can help me? I need to submit this project tomorrow
 
read line from a file, write that line to another file, if it doesn't start with 1
 
So copy the data to another file?
 
Thanks
 
@Toreno96 nah, the classes aren't polymorphic so it's not worth using inheritance there
I mean, sure Boost.Operator does it, but it's unlikely someone will use boost::addable<T> polymorphically
 
10:36 PM
And how can I get the number at the start of the line?
 
split the string with std::stringstream
although in this specific case you can simply check first two characters
 
Can you please show an example code?
Oh... Okay
Understood
 
60
Q: How to use stringstream to separate comma separated strings

MeysamI've got the following code: std::string str = "abc def,ghi"; std::stringstream ss(str); string token; while (ss >> token) { printf("%s\n", token.c_str()); } The output is: abc def,ghi So the stringstream::>> operator can separate strings by space but not by comma. Is there anywa...

 
Thanks a lot
 
@Toreno96 continuing: you need to check not if you are in invalid state, but whether you are about to enter invalid state
in this specific case you can check beforehand and revert it, but it's better to simply not happen
also your internal_ member is public
 
10:43 PM
@milleniumbug It is public deliberately, because it's just a toy example, for experimenting with Boost.Operators
 
about LSP violation: adding Digit(8) + Digit(3) results in a thrown exception where Integer(8) + Integer(3) would be valid
which means Digit isn't a proper derived class of Integer since it narrows preconditions
About exception classes: std::logic_error is more appropriate here
 
About LSP violation: wow, schools teach OOP so bad.
 
that's too common sadly
 
@milleniumbug I will try to fix everything. Would you look again, then? Thanks for everything so far :)
 
10:59 PM
feel free to post it, I probably won't be able to look at it today, but maybe someone else will
 

« first day (344 days earlier)      last day (2500 days later) »