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

std::ostringstream to get CString::GetBufferSetLength()-like functionality?

6
Hi there,
Can std::ostringstream be used to get CString::GetBufferSetLength()-like functionality? CString provides rather convenient way to use it as a buffer, in particular for winapi functions.

Expand|Select|Wrap|Line Numbers
  1. //just an illustration
  2. CString str;
  3. SHGetSpecialFolderPath( 0, str.GetBufferSetLength(..
  4. return str;
  5.  
Is it possible to do such a trick using std::ostringstream? // wostringstream, to be more accurate
Thanks in advance
Feb 9 '12 #1

✓ answered by weaknessforcats

So have the fuction return a string. In that case you have a vector<char> so you can use that as an argument to the string constructor:

Expand|Select|Wrap|Line Numbers
  1. string funct(vector<char>& data)
  2. {
  3.      //some processing
  4.      return string(&data[0], data.size());
  5. }

9 3592
weaknessforcats
9,208 Expert Mod 8TB
I would use an STL vector as a buffer since it is implemented as a array and you have access to the array. Then vector::capacity would get you the current buffer length.
Feb 9 '12 #2
ibox73
6
Thank you for your reply. The whole CString trick is that you can use say the stack string variable as a buffer and then return it, so no copying is fulfilled. Additional buffer can be declared as char[] equally well, but the problem is the ownership for it cannot be delegated to the return result AFAIK.
Feb 9 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
To avoid unnecessary copies use a reference.

Expand|Select|Wrap|Line Numbers
  1. vector<int> arr;
  2.  
  3. funct(arr);
  4.  
where

Expand|Select|Wrap|Line Numbers
  1. void funct(vector<int>& data)
  2. {
  3.  
  4. }
Inside the function funct, data is the same variable as arr. data is just an alias for arr. No copy is made on the call.
Feb 10 '12 #4
ibox73
6
The function I have to write must return string, not an array, so unfortunately this doesn't suit.
Feb 10 '12 #5
weaknessforcats
9,208 Expert Mod 8TB
So have the fuction return a string. In that case you have a vector<char> so you can use that as an argument to the string constructor:

Expand|Select|Wrap|Line Numbers
  1. string funct(vector<char>& data)
  2. {
  3.      //some processing
  4.      return string(&data[0], data.size());
  5. }
Feb 10 '12 #6
ibox73
6
[ -- server error -- ]
Jun 28 '12 #7
ibox73
6
[ -- server error -- ]
Jun 28 '12 #8
ibox73
6
Even I haven't found a std-based replacement for the old sweet CStrings's getbuffersetlength, - thank you very much, weaknessforcats, for your replies!
Jun 28 '12 #9
weaknessforcats
9,208 Expert Mod 8TB
getbuffersetlength is weakness of CString because it exposes the underlying implementation. When you use it you tie your application to that implementation forever.

Breaking your app while redesigning CString is a big no-no. Therefore CString is now frozen as far as you are concerned. The safe thing to do now is to embed the CString code in your app so you have control over the implementation you used to build the app.
Jun 28 '12 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Vikas | last post by:
Hi y'all, I need logging functionality to provide me following: - logging levels that can be set at run-time. - able to output to different files for different levels - thread safe - line,...
2
by: Tom | last post by:
Hello, I'm looking for a tool that would allow me to create a web page with the spreadsheet like functionality. Basically, I want to be able to type in a number in the cell and have all totals...
25
by: KK | last post by:
Hi, I am using history.go(-1) for implementing the back button functionality. Its working fine but with this exception. 1. The page which is having back button has some hyperlinks on it. ...
0
by: blongmire | last post by:
OS: WinXP Home Access 2000 Word 2000 I loaded 1998 vintage 16-bit business mapping software onto a PC and I think it overlayed some OS files dealing with DDE functionality. After that software...
0
by: rellik | last post by:
Hi All, I've run into a problem with the MenuStrip control and any help would be greatly appreciated! The problem I've got is that when use a control derived from the MenuStrip class all MDI...
1
by: Srini | last post by:
How do I do a functionality for a ASP.NET web application? Is there an automated way to do this? Can ACT do functionality test apart from the stress tests? When I make some change to the...
11
by: MadMonk | last post by:
Hi, I need to write a small Windows application (console or forms) (for Windows XP SP2 & Windows Server 2003), which will check a folder for the presence of some files, and if they exist, it...
23
by: Dautkhanov | last post by:
Hello ! Does anybody have cutted version of prototype.js with the AJAX functionality only? I am a new in prototype.js topic, so I think this task should be done by other developers. Maybe...
26
by: karthikbalaguru | last post by:
Hi, While trying to understand the difference between the following 2 methods, i have some interesting queries. Method 1) char *s = "Hello"; and Method 2) char s = "Hello"; How does the...
1
by: Monty | last post by:
I know this isn't necessarily an ASP.Net question, but it is web programming and I intend to try it using ASP.Net, I just need to get pointed in the right direction. I've seen some sites that allow...
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...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.