Hello,
I can initialized, e.g, heap-allocated ints by writing something like
new int(5).
Now I'd like to know if it is possible to do the same thing for struct
types. Say, I have a struct declared by the following lines:
struct S {
int i;
const char c;
}
I want to write something like
new S {5, 'd'}.
Is this possible?
Wolfgang 4 6758
Wolfgang Jeltsch escribió: types. Say, I have a struct declared by the following lines: struct S { int i; const char c; } I want to write something like new S {5, 'd'}. Is this possible?
You can add a constructor to S.
S (int i, const char c) : i (i), c (c) { }
And then use
new S (5, 'd');
Regards.
"Wolfgang Jeltsch" <je*****@tu-cottbus.de> wrote in message
news:bh************@ID-77306.news.uni-berlin.de... Hello,
I can initialized, e.g, heap-allocated ints by writing something like new int(5). Now I'd like to know if it is possible to do the same thing for struct types. Say, I have a struct declared by the following lines: struct S { int i; const char c;
Add a constructor:
S(int _i, char _c) : i(_i), c(_c);
} I want to write something like new S {5, 'd'}.
Make that new S(5, 'd');
HTH,
S. Armondi Is this possible?
Wolfgang
"Samuele Armondi" <sa****************@hotmail.com> wrote in message
news:3f********@mk-nntp-1.news.uk.worldonline.com... "Wolfgang Jeltsch" <je*****@tu-cottbus.de> wrote in message news:bh************@ID-77306.news.uni-berlin.de... Hello,
I can initialized, e.g, heap-allocated ints by writing something like new int(5). Now I'd like to know if it is possible to do the same thing for struct types. Say, I have a struct declared by the following lines: struct S { int i; const char c; Add a constructor: S(int _i, char _c) : i(_i), c(_c);
Sorry, typo slipped in... That should be:
S(int _i, char _c) : i(_i), c(_c) {}; } I want to write something like new S {5, 'd'}.
Make that new S(5, 'd');
HTH, S. Armondi Is this possible?
Wolfgang
Wolfgang Jeltsch <je*****@tu-cottbus.de> wrote in message
news:bh************@ID-77306.news.uni-berlin.de... Hello,
I can initialized, e.g, heap-allocated ints by writing something like new int(5). Now I'd like to know if it is possible to do the same thing for struct types. Say, I have a struct declared by the following lines: struct S { int i; const char c; } I want to write something like new S {5, 'd'}. Is this possible?
Yes, and the mechanism (knows as a 'constructor') does
not depend upon where the memory is allocated:
#include <iostream>
struct S
{
int i;
const char c;
S(int ii, char cc) : i(ii), c(cc) { } /* constructor */
};
int main()
{
/* automatic storage: */
S obj(5, 'd');
std::cout << obj.i << '\n'; /* prints 5 */
std::cout << obj.c << '\n'; /* prints d */
/* allocated storage: */
S *ptr = new S(5, 'd');
std::cout << ptr->i << '\n'; /* prints 5 */
std::cout << ptr->c << '\n'; /* prints d */
delete ptr;
return 0;
}
Read about constructors in any quality C++ textbook.
-Mike This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: mordac |
last post by:
Hello, I was wondering if I could get some opinions on how best to
handle printing in a max heap data structure. Right now my heap
struct looks as thus:
typedef struct heapStruct {
int*...
|
by: GrandpaB |
last post by:
While writing this plea for help, I think I solved my
dilemma, but I don't know why the statement that solved
the problem is necessary. The inspiration for the
statement came from an undocumented...
|
by: Dennis Jones |
last post by:
Hi,
I have some old code that I am refactoring to use smart pointers and have
run into a small problem. My original code looks something like this:
class WorkerThread
{
std::map<int,...
|
by: JurgenvonOerthel |
last post by:
Consider the classes Base, Derived1 and Derived2. Both Derived1 and
Derived2 derive publicly from Base.
Given a 'const Base &input' I want to initialize a 'const Derived1
&output'.
If the...
|
by: JosAH |
last post by:
Greetings,
I was asked to write a Tip Of the Week; so here goes: a lot of topics are
started here in this forum (and a lot of other forums too) mentioning a
problem about sorting data.
...
|
by: kumarmdb2 |
last post by:
Hi guys,
For last few days we are getting out of private memory error. We have a
development environment. We tried to figure out the problem but we
believe that it might be related to the OS...
|
by: Jason Doucette |
last post by:
Situation:
I have a simple struct that, say, holds a color (R, G, and B). I
created my own constructors to ease its creation. As a result, I lose
the default constructor. I dislike this, but...
|
by: WaterWalk |
last post by:
Hello. When I consult the ISO C++ standard, I notice that in
paragraph 3.6.2.1, the standard states:
"Objects with static storage duration shall be zero-initialized before
any other...
|
by: Martin Payne |
last post by:
I am trying to initialise a structure with random values for all its
fields. It is a large structure so I do not want to do it for each
element in turn (it would take ages).
Please note my...
|
by: ggoubb |
last post by:
The purpose of the Insert function is to add a new integer in the Heap assuming that it is not already full. If Heap capacity has been reached, it attempts to double the current capacity. If capacity...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
| |