C++ :: Custom Exceptions With Default Behaviour

May 24, 2013

I've been pondering which of these 2 approaches would make for the best interface for a library: Defining custom exceptions with specific names for different error scenarios but with standard behaviour, or simply using the predefined exceptions from the STL.

This is my current approach:
Code:
namespace rpp
{
class ConnectionError : public std::exception
{
public:
ConnectionError(const std::string &p_err);

[Code] .....

This seems to make for more descriptive code but it adds no functionality and the implementations are completely identical, which seems "off" to me, somehow.

View 8 Replies


ADVERTISEMENT

C :: Implementing Custom Behaviour But Something Fails

Jan 2, 2015

I am creating a special struct with unknown functions. I use this approach:

Callbacks.h Code: #ifndef CALLBACKS
#define CALLBACKS
struct Callbacks;
struct Callbacks* getNewCallback();

[Code]....

When I ran it only the calls from doers array is called 7 times normally, and donters only one time. Why is that? When I call doers from the second loop, it prints the doers functions again....and only one call to donters is made to the first static inline donter functions __dont1()...

View 7 Replies View Related

C++ :: Change Behaviour Of Template Class Depending On Type?

Jul 8, 2012

is there a way to have a template class respond to missing stuff in a template type ?

Code:
template <typename Type>
class MyClass
{
public:
enum { ID = Type::ID }; // revert to 1 if Type::ID doesn't exist.
};

If the Type passed to the template has an ID member (required to be an enum or a static const int), use it, if it is missing revert to a default value.

I can use this as a simplified way of configuring how MyClass works, without requiring Type to explicitely needing to define what it doesn't care about.

It needs to be resolved at compiletime, as it determines the number of elements in member array variables.

View 10 Replies View Related

C++ :: Developing Application Without Exceptions

Feb 27, 2015

Is it possible to develop any application without exceptions? I don't want to try and catch exceptions and remove it.but I want to write codes which should never create exceptions is it possible?

View 3 Replies View Related

C++ :: Exceptions Thrown During Initialization?

Jul 8, 2014

How exactly would one go about throwing an exception from an object's constructor?

Take a look at this snippet:

#include <iostream>
#include <exception>
class ExceptionFoo : public std::exception {
virtual const char* what() const throw() {

[Code].....

View 8 Replies View Related

C++ :: Assignment Operator But With Some Member Exceptions

Jan 9, 2015

The task is to use the assignment operator of a class, but change all the data except certain ones. For example, below we are to assign all Person data of 'other' except for 'name' and 'ID':

#include <iostream>
#include <string>
struct Person {
std::string name;
int ID, age, height, weight;

[Code] .....

Name = Bob
ID = 2047
Age = 38
Height = 183
Weight = 170

Name = Frank
ID = 5025
Age = 25
Height = 190
Weight = 205

Bob pretends to be Frank, but keeps his name and ID.

Name = Bob
ID = 2047
Age = 25
Height = 190
Weight = 205

But I think the way I did it is pretty lousy (note the wasted steps changing the name and ID only to revert them back? So the ideal solution should require no wasted steps, unlike the method above, and changes to what the exclusions should be should be in only one place (not two like above). Of course, we assume that Person shall have many, many data members (and constantly increasing), so that simply defining Person::operator= (const Person& other) to handle all data except for 'name' and 'ID' is out of the question.

View 3 Replies View Related

C++ :: Catching Exceptions From Initializer List

Jan 4, 2015

In order to test catching exceptions from an initializer list, I deliberately did bad practice by hard coding an argument to a ctor that would cause a std::bad_allocto be thrown. Obviously better practice is to send a variable, but that would cause a compile error, so I hard coded a value.

The program I wrote creates Prime Numbers up to a specified limit which is an argument to the ctor of type std::size_t. The program works fine IMO, using g++ in cygwin:

$ time ./PrimesExe
Limit is 2000000
148933 Primes Created

real 0m1.210s
user 0m1.123s
sys 0m0.046s

Now when I send something invalid like a negative number or something too big for std::size_t, the program seems to run indefinitely, when compiled with g++ under cygwin. I haven't tested it yet on Linux.

However, if I do the same on VS2013 express, it takes about 15 seconds to print the expected caught exception message. I was not expecting it to take so ridiculously long compared to the reasonable amount of work involved in doing primes up to 2 million.

I have read up about what is involved in catching exceptions: stack unwinding, keeping track of what needs to be destroyed etc. But this is 1 object with 1 ctor argument, no Base classes or any other complications. So why such a long or indefinite amount of time?

This whole example is probably contrived, and I am wondering whether exceptions is the right tool for this - it is similar to the divide by zero problem, or could be considered a programming error to call a ctor with a bad argument?

Also, catching an exception thrown by an initalizer list seems a bit awkward in that one seems to have enclose the creation of the object and all subsequent uses of it (and any code in between ) in the same try block, otherwise it goes out scope. I suppose I could try to write a wrapper function that returns a smart pointer reference to a valid object, but I would have to test the validity of it's return too. That's the awkward part - there is probably a better way?

Are there any recommended ways of recovering from initializer list exception, that is, to allow the user to enter a new hopefully valid value and try to create the object again?

View 2 Replies View Related

C++ :: How To Handle The Exceptions Not Caught By Try / Catch

Nov 2, 2014

Here is an example,

Code:

int main()
{
try{
int y = 0;
int x = 2/y;
}
catch(...)
{
cout<<"catch it"<<endl;
}
return 0;
}

If you try, you will find out that catch(...) couldn't catch exception divided by zero. How'd we catch such exceptions?

View 4 Replies View Related

Visual C++ :: Getting Exceptions On Windows 7 And 8 On Different Parts Of Code?

Feb 25, 2015

I'm Getting exceptions on Windows 7 and Windows 8 on different parts of the Code? Why this can be happening, why the difference in behavior?

View 6 Replies View Related

C :: Default Value Of Pointer

Feb 28, 2014

Following is the code snippet

Code:
[COLOR=white !important]?
1
2
3
4
5 char str1[]="Bombay";
char str2[]="Pune";
char *s1=str,*s2=str2;
while(*s1++=*s2=str2);
printf("%s",str1);

Output of this code comes out to be Pune

But according to me output should be puneay.

Pune should be copied in place of Bomb.and rest should be as it is.
[/COLOR]

View 12 Replies View Related

C++ :: Set Pointers To Default Value For Both 32 Bit And 64 Bit

Sep 6, 2013

I have a problem. I want to set pointers to a default value for both 32 bit and 64 bit compiles. The 32-bit version works as:

enum constants { UNDEFINED = 0xDeadBeef }
if ((unsigned long)ptr == UNDEFINED)

but I can't seem to extend this to 64-bits. I've tried
#if __SIZEOF_POINTER__ == 4
enum constants { UNDEFDATA = 0xDeadBeef };
}; // enum constants
#elif __SIZEOF_POINTER__ == 8
enum constants { UNDEFDATA = 0xDeadBeefDeadBeef };
#endif

with:
if (ptr == UNDEFINED)

but get a message saying the '==' is undefined (I understand this)

Is there any way to setup so that I can change the size of my constants so that the comparisons will always work correctly? I've tried a 'typedef' but the compiler complains at

'typedef unsigned long long ADDR' // won't accept, and
static const SlipCellBase * const TEMPORARY = (SlipCellBase&)0xFFFFFFFFFFFFFFFF; // illegal conversion

enum doesn't work (because it's an int?)

View 1 Replies View Related

C++ :: No Appropriate Default Constructor Available?

Oct 12, 2014

First I wrote a Binary-tree class to draw a binary tree on the window. The nodes were small circles. Then I become wanted to change the shape of the nodes from circles to triangles by another class, Binary-tree-derived which is derived from Binary-tree class. I wrote the below code to do the job but I get two errors about constructors. First, code:

/* The binary-tree class, one of the topics that has been said in Programming Principles and Practice using C++ book by Biarne Stroustrup.
This code is written by R Abbasi (s.rabbasi@yahoo.com) */
#include <Simple_window.h>

[Code].....

Errors are:

Error12error C2512: 'Binary_tree' : no appropriate default constructor availablec:userscsdocumentsvisual studio 2012projects est_1 est_1 est_1.cpp91

13IntelliSense: no default constructor exists for class "Binary_tree"c:UsersCSDocumentsVisual Studio 2012Projects est_1 est_1 est_1.cpp91

View 4 Replies View Related

C :: How To Create Default Arguments

Oct 20, 2013

How to create default arguments in C? Is there any way to make default arguments ( i mean any alternative for them).

View 5 Replies View Related

C++ ::  How To Write A Default Constructor

Sep 27, 2013

How do you write a default constructor?I need to use a default constructor that will initialize the data members:
- salesPerson to “Unknown”
- noOfWeek to 13
- amount to point to an array of thirteen 0.00s.

This is my weeklysales class

class WeeklySales {
char* salesPerson;
double* amount; // Pointer to an array
int noOfWeek; // Size of the array
};

Here's my attempted code:

//default constructor
WeeklySales (){
salesPerson="Unknown";
noOfWeek = 13;
amount = 0.00;
}

View 9 Replies View Related

C++ :: Synthesized Default Constructor

Dec 25, 2013

A compiler auto created default constructor is called a synthesized default constructor. It will initialize the built-in members to 0 or not depends on where the class object is defined? if I define a class

class point{
public:
double x, y;
};

if I define point point1; in global scope then point1.x and point1.y will be initialized to 0, if I define point point2; in a local scope, then its x and y won't be initialized? If it is like this, then I believe if there are built-in type members in a class, then the synthesized default constructor is almost useless!

View 3 Replies View Related

C++ :: Multiple Default Constructors Specified

Dec 16, 2014

I have an inherited class that essentially manages a Qt Window.

It is as follows (prototype below):

class QTMyOpenGLWindow : public QWindow, protected QOpenGLFunctions {
Q_OBJECT

[Code] ....

Now, I can understand the confusion of the compiler, but the functionality as I laid it out works for me (I can create the class with just specifying the parent and also have the option of preventing auto-initialization when creating). But, is there a better approach?

View 3 Replies View Related

C++ :: Default Arguments And Omitting Only Some Of Them?

Apr 7, 2014

I am trying to figure out what's the best way to accomplish something like this:

void function(t1 a=0,t2 b=0){/*...*/};
t2 value;
function(value);

That would throw a compile error, since the first argument that is being passed to the function (value) is considered the first argument in the declaration (a), which is of type t1. So, is there a way to force my function to consider value as the second argument instead of the first one? I am aware that this could be done using overloading, but the larger the amount of arguments, the larger amount of possibilities, so it might end up with a huge list of overloads. The best case scenario would be being able to set things like:

void function(t1 a=16,t1 b=0,t2 c=1){/*body*/};
function(b=3,a=0);
but I'm not aware of such feature in C++.

Would it be possible to design some sort of macro system to take care of this?

View 10 Replies View Related

C++ :: Where Is DirectX SDK Installed By Default

Feb 13, 2013

So a long time ago I messed around with DirectX but I don't remember where it gets placed by default. I want to just get rid of the SDK and get the up to date version of it.

View 4 Replies View Related

C++ :: Application Default Parameters?

Mar 16, 2013

I have developed an application in C++ that creates some text files in a directory chosen by the user.

How can I ask the user set a Default Directory Path (and some other default parameters) so that she doesn't have to enter the same data in the GUI everytime the application is run.

The application has been developed using Qt Creator.

View 3 Replies View Related

C++ :: Template Class With Default Value?

Mar 22, 2013

refer to the code below, the attribute class is created with the value type and default value, but why it doesn't work for std::string?

#include <string>
#include <iostream>
using namespace std;

[Code].....

View 9 Replies View Related

C++ :: What Is Default Size Of STL Vector

Jan 30, 2012

For example, I have an empty vector of integer. If I keep calling push_back on vector, is it going to be out of memory?

View 8 Replies View Related

C++ :: Implicit Default Ctor - What Is It For

Apr 16, 2013

Implicit default ctor does not initialize the built-in data members, so what is it needed for?

View 5 Replies View Related

C++ :: How To Build A Custom Allocator

Feb 7, 2014

I'm trying to write a custom allocator that I can use with the STL. Here's what I have so far :

Code:
#include <cstddef>
#include <iostream>
#include <memory>
#include <vector>
template<class T>
struct customallocator {

[Code].....

I'm doing and currently, my push_back doesn't seem to do anything.

View 12 Replies View Related

C++ :: Custom Vector Class

Oct 9, 2014

Let's say we have a custom Vector class and I need to know which of the following is considered to be more efficient and why of course.

Vector Vector::operator+(const Vector &b) const {
return Vector(x+b.x,y+b.y);
}

Vector Vector::operator+(const Vector &b) const {
Vector tmp(x+b.x,y+b.y);
return tmp;
}

View 1 Replies View Related

C++ :: Using STL Sort With Custom Classes?

Oct 8, 2013

I've written a doubly linked list per my assignment instructions. I've implemented begin() and end() iterators for it, and they work with no problems when traversing the list. However, I need to sort the elements in the list. We are allowed to use the sort function defined in the <algorithm> header since we haven't yet covered sorting algorithms.

But, I'm running into a ton of problems. I figured as long as the begin() and end() iterators were defined for the list, then sort(list.begin(), list.end(), compare) would do the trick. The main errors I'm getting are:

error: no type named iterator_category
error: no type named value_type
error: no type named difference_type
error: no type named pointer
error: no type named reference

And also errors for no match of the + and - operators for the iterator class.

I understand reference, pointer, and value_type, but I have no idea about iterator_category and difference_type. Additionally, I'm a little unsure why the + and - operators need to be overloaded.

View 8 Replies View Related

C++ :: Add Custom MENUITEM To A POPUP?

Jul 13, 2014

I have a POPUP

MAIN_MENU MENU {
POPUP "&Manager" {
MENUITEM "New Royal Python", MENU_NEW_ROYAL_PYTHON
POPUP "&Select Python" {
}
}
}

I basically want to give the POPUP an ID so I can talk with it but it does not seem to be accepting a second parameter. Once I have done that I need to send a message to it to ask it to add a new item, what are the messages I need to send for this?

View 8 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved