473,385 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

operator new and parameterized constructor

Hi, I was wondering if anyone knows how to overload operator new to
use a parameterized constructor.

Something like this:

class class1
{
public:
class1();
class1(int parameter1);

}

int main()
{
int number=32;
class1 * temp;
temp= new class1[number](parameter1);
}

Thanks
Jul 19 '05 #1
2 5897

"Daniel Fortin" <da************@hotmail.com> wrote in message
news:d3**************************@posting.google.c om...
Hi, I was wondering if anyone knows how to overload operator new to
use a parameterized constructor.

Something like this:

class class1
{
public:
class1();
class1(int parameter1);

}

int main()
{
int number=32;
class1 * temp;
temp= new class1[number](parameter1);
}


I believe the restrictions on overloading operator 'new[]'
do not allow this. But you could write a function to achieve it:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <iterator>

template <typename T, typename PT>
T *initialized_alloc(std::size_t count, PT arg)
{
T *p = new T[count];
std::fill(p, p + count, arg);
return p;
}

class C
{
int mem;

public:
C() {} /* 'new[]' needs a default ctor */
C(int arg) : mem(arg) { } /* ctor with param */

operator int() const { return mem; } /* facilitates 'simple' */
/* output below */
};

int main()
{
std::size_t how_many(10);

C *p = initialized_alloc<C>(how_many, 42);

std::copy(p, p + how_many,
std::ostream_iterator<int>(std::cout, "\n"));

delete[] p;
return 0;
}

But this might be more trouble than it's worth. :-)

-Mike
Jul 19 '05 #2
Mike Wahler wrote:

"Daniel Fortin" <da************@hotmail.com> wrote in message
news:d3**************************@posting.google.c om...
Hi, I was wondering if anyone knows how to overload operator new to
use a parameterized constructor.

Something like this:

class class1
{
public:
class1();
class1(int parameter1);

}

int main()
{
int number=32;
class1 * temp;
temp= new class1[number](parameter1);
}


I believe the restrictions on overloading operator 'new[]'
do not allow this. But you could write a function to achieve it:


Or just use std::vector:

std::vector<class1>(number, class1(parameter1));

However, that does not initialize each element with parameter1, but
rather copy-construct them from the temporary you give as argument.

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Emanuel Ziegler | last post by:
Hello, I want to do some mathematics with functions. In my case the function classes are very complex, but this simple example has the same problems. To allow calculations that begin with a...
4
by: August1 | last post by:
I've written an interface and implementation file along with a client source file that allows the use of an overloaded subtraction operator. However, when using the program, I'm running into a...
2
by: Stig Nielsson | last post by:
I am wondering what is the most efficient way to make a parameterized factory method, and before I start spending time on performance measurements myself, I would like to hear the NG's opinion: ...
1
by: Peter | last post by:
Hi, When I update/add a new web reference to a web service class with a parameterized constructor, what is preventing the generated proxy from including a definition for the parameterized...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
5
by: Sathyaish | last post by:
/* I am writing this C# program because I forgot the syntax for calling a parameterized constructor from a non-parameterized one within the same class. I was coding in Java and just typed it...
10
by: siddhu | last post by:
Dear Experts, I want to make a class whose objects can be created only on heap. I used the following approach. class ss { ss(){} public: void* operator new(size_t sz)
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.