Discussion: Correct C++

Pages: 12
typedef introduces an alias for a type in a given scope. In the exmple I showed 'I' becomes an alias for the type int. So it is equivalent to write

int x;
and

1
2
3
typedef int I;

I x;
Last edited on
Great! Now it's three ways of giving a value to Int. But which one beeing the most correct C++ remains...
I answered your question in my first message in this thread.
It's an opinion, not an answer?
If C does not allow one or the other does not answer if the later is more correct.

(in my opinion)

=)
Last edited on
C and C++ are two diffferent languages. You asked about C++. And you got the answer.
Which is more correct, char* strings, char[] strings or std::strings?
Last edited on

@samrux
Which is more correct, char* strings, char[] strings or std::strings?


I would ask the question in another way what declaration of the two is more correct?:)

char s[] = "Hello";
ot
char s[] = { "Hello" };
Last edited on
Where T is a type and e is an expression (of some type other than T),

T x(e) ; or T x{e} is 'direct initialization'

T x = e ; is 'copy initialization'.

1
2
3
4
5
struct A { explicit operator int() const { return 0 ; } } ;
A a ;

int x(a) ; // fine; direct initialization, 'a' explicitly converted to int
int y = a ; // *** error; copy initialization, no implicit conversion from 'a' to int 


There is no practical difference between the two unless a user defined type is involved.
Last edited on
@vlad from moscow

I mean:
1
2
3
char Example1[99] = "Hello";
char* Example2 = "How are you";
string Example3 = "I'm bored";
Last edited on
The only way of something to not be correct C++ is if it doesn't conform to the standard.

all of those 3 are valid, the first two are C-Strings and the latter is a C++ string, so if you want to right 'as little C as possible' the last one is 'more correct'
> all of those 3 are valid

1
2
char* Example2 = "How are you"; 
//warning: deprecated conversion from string constant to 'char*' 

http://c-faq.com/decl/strlitinit.html
My bad, I didn't bother checking.

@Zephilinox
...all of those 3 are valid, the first two are C-Strings and the latter is a C++ string,...


The first is a character array. The second is a pointer to char that shall be declared as a pointer to const char. And the third is an object of type std::string.



Topic archived. No new replies allowed.
Pages: 12