Tuesday, November 19, 2013

Is this C or C++?

Newsgroup: comp.lang.c++

Subject: Is this C or C++?

From: "crea" <no@...>

Date: Tue, 19 Nov 2013 21:44:40 -0000



Simple question. If the task is (for example):

"Write a C++ program which asks user his name (less than 20 chars) and

prints it."



Then, is this code a correct answer:



char name[100];

cout<<"Your name?"<<endl;

cin>>name;

cout<<name<<endl;



The point being, that the code uses C string "char name[]" and not C++

std::string.

C is a subset of C++, so isnt it logically speaking a C++ program?











via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?131968-Is-this-C-or-C&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

converting double to int

Newsgroup: comp.lang.c++

Subject: converting double to int

From: ram@...

Date: 19 Nov 2013 12:23:40 GMT



#include <iostream>

#include <ostream>



int main()

{ ::std::cout << int( 2.6 )<< '\n';

::std::cout << int{ 2.6 }<< '\n';

::std::cout << ( int )2.6 << '\n';

::std::cout << static_cast< int >( 2.6 )<< '\n'; }



Under which circumstances is which way of the above to

convert a double to an int the best style?



Are there any differences in the semantics above?



Is there yet another way to write the conversion?



PS: Oh yeah: the int{} notation forbids narrowing!

I should have compiled with -pedantic-errors!

So, int{} differs from the rest, it is not a kind of

cast or conversion, rather a compile-time assertion

of the number being not wider than int?



Is the number ?2.6? an argument or an operand in

?int{ 2.6 }?? It does not seem to be a function

call. So it is not an argument. But is this an

application of an operator? No. So it also is not

an operand? So, what is it then?









via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?131627-converting-double-to-int&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

Monday, November 18, 2013

Exceptions in class

Newsgroup: comp.lang.c++

Subject: Exceptions in class

From: mymailaddressin@...

Date: Mon, 18 Nov 2013 10:33:43 -0800 (PST)



im trying to handling exception in my matriz class. I have no idea how this suppoosed to be



My matriz class its like this



#ifndef Matriz_H

#define Matriz_H



#include <iostream>

#include <iomanip>



using namespace std;



template <typename T> class Matriz ;

template <typename T> std::istream& operator >> ( std::istream &is_in,Matriz <T> &M);

template <typename T> std::ostream& operator << ( std::ostream &os_out, const Matriz <T> &M);



template <typename T>

class Matriz{

friend ostream &operator << <> (ostream &, const Matriz <T> &);

friend istream &operator >> <> (istream &, Matriz <T> &);



public:

Matriz (int = 5, int = 5);

Matriz (const Matriz &);

~Matriz ();



void setSize (int = 5, int = 5);



const Matriz &operator = (const Matriz &);

bool operator == (const Matriz &) const;

Matriz operator * (const Matriz &);

T &operator () (int, int);



private:

int nrows;

int ncols;

T **pntr;

};

#endif



im trying to make the out of range exception in



template <typename T> T &Matriz <T> :: operator()(int rws, int clm) {



// try{

return pntr[rws][clm];

// }

// catch{

// cout << "Out Of Range" << endl;

// }

}

also i would like to know how to handle the invalid type exception



Thanks in advance









via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?131067-Exceptions-in-class&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

Where is RAII when you need it?

Newsgroup: comp.lang.c++

Subject: Where is RAII when you need it?

From: ram@...

Date: 17 Nov 2013 19:19:07 GMT



Today, I wanted to show RAII, like this:



#include <iostream>

#include <ostream>

#include <stdexcept>



struct K

{ K (){ ::std::cerr << "K" << ::std::endl; }

~K(){ ::std::cerr << "~K" << ::std::endl; }};



struct R

{ K * k; R() : k{ new K{} }{}

~R() { ::std::cerr << "~R" << ::std::endl; delete k; }};



int main(){ R r{}; throw ::std::runtime_error( "error" ); }



The output was:



K

terminate called after throwing an instance of 'std::runtime_error'

what(): error



This application has requested the Runtime to terminate it in an unusual way.

Please contact the application's support team for more information.



--------------------------------

Process exited with return value 3

Press any key to continue . . .



No ~K nor ~R in sight! Why?







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?130533-Where-is-RAII-when-you-need-it&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

Friday, November 15, 2013

vtables for dynamic dispatch - an "invention"?

Newsgroup: comp.lang.c++

Subject: vtables for dynamic dispatch - an "invention"?

From: ram@...

Date: 15 Nov 2013 19:30:42 GMT



I read someone "invented" vtables. But to me it seems that

they are obvious in the sense that a class is a list of

methods (simplified), which gives the vtable, and an object

has a class, which means that the object holds a pointer to

a vtable.



I cannot think of another means to implement virtual member

functions.



Is there something non-obvious about vtables that has to be

"invented"?



Are there other reasonable possibilities to implement

virtual calls?









via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?129166-vtables-for-dynamic-dispatch-an-invention&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

Thursday, November 14, 2013

Returning a class instance by value and a naming question

Newsgroup: comp.lang.c++

Subject: Returning a class instance by value and a naming question

From: DSF <notavalid@...>

Date: Wed, 13 Nov 2013 18:09:08 -0500



Hello,



I have a class I've been writing where I frequently need to return

the class by value so that class members can be changed without

affecting the original. The problem is all of the copying. First,

the class has to be copied into a local instance of the class. Then,

upon return, the local instance is copied into an unnamed instance

that's created before the function is called, and therefore not

destroyed when the function ends. Finally, after the function ends,

the operator= of the target is called, yet again copying the class

(unnamed). Here is an example:



FStringW FStringW::Start(size_t end) const

{

FStringW tfs(*this);

if(end < length)

{

tfs.str[end] = 0;

tfs.length = end;

}

tfs.StartIP(end);

return tfs;

}



StartIP is a member function that alters the string in place.



It returns 'end' number of characters of the start of a wide string.



I tried creating a "global" temp that could be used in place of each

function that used 'tfs'. It was a pointer to FStringW that was

created in each constructor and deleted in the destructor. I managed

to avoid an infinite loop in the constructor, but didn't realize it

created one in the destructor, too. I wasn't able to get around that

one. All the functions that returned FStringW returned a reference to

the global object, so the reference had the lifetime of until another

function uses it. That was acceptable. It would have eliminated one

copy if it worked.



So the question is: Are there any techniques for minimizing the

number of copies that occur when returning by value?



The ideal situation would be if the returned object could become the

unnamed object that's passed to operator=. But I think the compiler

would have to do that. And there's always the chance the function

could return an expression i.e. return tfs1 + tfs2.



My second question relates to the naming of member functions. Most

string-altering functions I've written have two versions, one that

returns by value and leaves the original alone, and one that alters

the string in place. For now, I'm using:



FStringW FStringW::Start(size_t end);

void FStringW::StartIP(size_t end);



But I was thinking maybe something like:



FStringW FStringW::GetStringStart(size_t end);

void FStringW::StringStart(size_t end);



Something like Crop or Cut StringEnd is more descriptive, but uses

start and end as terms for functions that do very similar things. Any

Ideas?



Thanks.

"'Later' is the beginning of what's not to be."

D.S. Fiscus







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?128115-Returning-a-class-instance-by-value-and-a-naming-question&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

Monday, November 11, 2013

Reference is not an object.

Newsgroup: comp.lang.c++

Subject: Reference is not an object.

From: wy <warmyouth@...>

Date: Mon, 11 Nov 2013 09:08:32 +0800



I'm reading "C++ Primer", and it emphasizes "reference is not an object".

In P51, it says "Because references are not objects, we may not define a

reference to a reference." And in P52, it says "Because references are

not objects, they don't have addresses. Hence, we may not define a

pointer to a reference."



But the following code works with g++.



#include <iostream>



using namespace std;



int main()

{

int val = 0xfe;

int &ref= val;

int &r = ref;

int *p = &ref;

cout << "val = " << val << endl;

cout << "ref = " << ref << endl;

cout << "r = " << r << endl;

cout << "*p = " << *p << endl;

return 0;

}



Is the feature not standard, or do I misunderstand?







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?126005-Reference-is-not-an-object&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums