473,378 Members | 1,410 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,378 software developers and data experts.

_bstr_t behaviour (how it should work in ANSI C++)

Hi,

I hope I am not posting in the wrong place! I have posted to the MFC
group but thought I might get more of a response here as the question
is C++ specific. If I am wrong to post here or violating some rule
please let me know and I promise I won't do it again :)

I am using MFC/COM _bstr_t() in Visual C++ 6.0 and I am insterested to
know what constructors get called when the following call is made:

int main()
{
_bstr_t b = _bstr_t("Hello world");
return 0;

}

I realise VC++ is old and not standards-compliant, so forgetting that
for a moment I just want to make sure what I think should happen is
what would happen in ANSI C++!

I would expect the constructor which accepts a const char* would be
called which would return a temporary object then the copy-constructor
would be called to assign the temporary to "b".

When I run the debugger, the const char* constructor is called but no
copy-constructor! At first I thought this may be a compiler
optimization of some sort, or maybe some obsure feature of Visual C++
6.0 so I made a dummy class to test my theory and I was correct - the
copy constructor is called for my test class, so why not for _bstr_t?

Nov 29 '05 #1
1 2643
Paul Drummond <pa***********@dsl.pipex.com> wrote:
I am using MFC/COM _bstr_t() in Visual C++ 6.0 and I am insterested to
know what constructors get called when the following call is made:
_bstr_t has nothing to do with MFC nor ATL. It belongs to "builtin COM
support", or more acurately set of headers distributed with the
compiler, which are used by some compiler extensions that we are not
going to discuss here.
_bstr_t b = _bstr_t("Hello world"); I would expect the constructor which accepts a const char* would be
called which would return a temporary object then the copy-constructor
would be called to assign the temporary to "b".
you are almost right. However C++ standard has an extra clause (12.8)
that allows (but does not require) temporary object to be entirely
eliminated when it's being copied around. Even if it changes behaviour
of the program in respect to copy constructor calls. This is why you
shouldn't depend on copy constructor being called, and this is why the
only thing copy constructor may do reliably is to perform a copy.
copy constructor is called for my test class, so why not for _bstr_t?


because compiler decided that it may optimize-away temporary object of
_bstr_t type in this context. Next version of the compiler might do this
to your type, and it is standard conforming behaviour
B.
--
Czy wiesz co to jest MIME? Zobacz http://www.grush.one.pl/mime

Nov 29 '05 #2

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

Similar topics

25
by: Nitin Bhardwaj | last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a student and have been doing & studying C programming for the last 4 years).....and also a regular reader of "comp.lang.c" I...
4
by: gautam | last post by:
can anyone pls tell me y is the memory allocation not alligned to 4 Bytes. Note : compiled with gcc in linux 9 void function(int a,int b,int c) { char buffer1; Bytes allocated(as shown by gdb)...
13
by: Chris Croughton | last post by:
Is the following code standard-compliant, and if so what should it do? And where in the standard defines the behaviour? #include <stdio.h> #define DEF defined XXX int main(void) { int...
31
by: grid | last post by:
Hi, A collegue of mine is of the opinion that the behaviour of the following program is defined,but I am a little apprehensive. #include<stdio.h> #include<string.h> int main() { char *c;
5
by: bluter | last post by:
We have server components which were created by a third party and compiled in VC++5 (sp3). They run fine on NT4 and 2000, however during testing of our migration to Server 2003, these components...
4
by: ashish_chap | last post by:
Hi, I am using _bstr_t class in a function. This is used in an application that is used in an multi-threaded environment. The function is implemented as follows: int Function(wchar_t *sqlCmd)...
10
by: Robert | last post by:
How do you get an accurate count of the number of records returned from a query when using linked tables. I have an access 2003 database as a front end to another access 2003 database that...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
8
by: arnuld | last post by:
my friend sent me this programme: #include<iostream> int main() { int a=5; std ::cout << ++a << "\t" << ++a <<
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
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...

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.